CMSC130 Introduction to Computer Programming I
Quiz: Java Introduction
Name:
Put your answers in the box below
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
1. Which of the following is the correct signature for the entry point of a Java program?
(A) public static void main(String[] args)
(B) public void main(String args)
(C) static public int main(String[] args)
(D) public static void Main(String[] args)
2. What is the exact output produced by the following program? (spacing and newlines
matter)
public class Welcome {
public static void main(String[] args) {
[Link]("Hello,");
[Link]("World!");
}
}
(A) Hello, World!
(B) Hello,
World!
(C) Hello, World! (two spaces)
(D) Hello, World
3. What is printed by the following?
public class PrintSect {
public static void main(String[] args) {
[Link](5 + 2 + "" );
}
}
1
(A) 7
(B) 52
(C) 5 + 2
(D) 57
4. What does this program print?
public class PrintSect {
public static void main(String[] args) {
[Link](10 / 3);
}
}
(A) 3.3333333
(B) 3
(C) 3.0
(D) 03
5. Running the following code will result in which outcome?
public class ErrorsSect {
public static void main(String[] args) {
[Link]("10 / 0 " + (10 / 0));
}
}
(A) It prints: 10 / 0 0
(B) It prints: Infinity
(C) It causes an ArithmeticException at runtime
(D) It fails compilation due to syntax error
6. What happens when compiling the following class?
public class JavaSect {
public static void main(String[] args) {
[Link]("Hi")
}
}
(A) Compiles and runs, printing: Hi
(B) Compiles but throws a NullPointerException at runtime
(C) Compilation error due to a missing semicolon
(D) Compilation error: class name must match file name
7. What is the output? (print does not append a newline, println does.)
2
public class PrintSect {
public static void main(String[] args) {
[Link]("ABC");
[Link]("DEF");
}
}
(A) ABC on one line, then DEF on the next line
(B) ABCDEF on one line only
(C) A\nB\nC\nDEF (each letter on its own line, then DEF)
(D) ABCDEF on two lines with a blank line between them
8. Which statement correctly prints a backslash followed by the letter n (i.e.,
n) literally?
(A) [Link]("\n");
(B) [Link]("\\n");
(C) [Link]("\\"); [Link]("n");
(D) [Link](" ");
9. What is the output of this program?
public class DrawSquare {
public static void main(String[] args) {
[Link]("******");
[Link]("* *");
[Link]("* *");
[Link]("******");
}
}
(A) A 4x4 square made of \* with hollow interior
(B) A 4x4 solid square of \*
(C) A triangle of \* (right-angled)
(D) A single line of 6 \* characters
10. What does the program print?
public class PrintSect {
public static void main(String[] args) {
[Link](12 + "" + 34);
}
}
(A) The sum 12
(B) The string 34
3
(C) The string 1234
(D) The sum 46
11. Which statement about Java whitespace/indentation is true?
(A) Indentation changes how Java groups statements
(B) Extra spaces inside strings are ignored
(C) Newlines can always replace semicolons
(D) Braces \{\} determine blocks, not indentation
12. Given left-to-right evaluation, what is printed?
public class PrintSect {
public static void main(String[] args) {
[Link]("10 + 20 = " + 10 + 20);
}
}
(A) 10 + 20 = 30
(B) 30 = 10 + 20
(C) 10 + 20 = 1020
(D) 10 + 20 = 30.0
13. Which output results from the code below?
public class PrintSect {
public static void main(String[] args) {
[Link]("10 + 20 = " + (10 + 20));
}
}
(A) 10 + 20 = 1020
(B) 10 + 20 = 30
(C) 30 = 10 + 20
(D) 10 30 20
14. How many lines are printed?
public class Welcome {
public static void main(String[] args) {
[Link]("A");
[Link]("B");
[Link]("C");
[Link]("D");
}
}
4
(A) 1
(B) 2
(C) 3
(D) 4
15. What is printed?
public class PrintSect {
public static void main(String[] args) {
[Link]("10/3");
}
}
(A) 3.3333333
(B) 3
(C) 3.0
(D) 10/3
16. What happens when compiling the following code? (focus on syntax)
public class Welcome {
public static void main(String[] args) {
[Link]("Hello);
}
}
(A) Compiles and prints Hello
(B) Compiles but throws a runtime exception
(C) Compilation error due to an unterminated string literal
(D) Compilation error: missing class modifier
17. The following code intends to print four asterisks on one line. What kind of issue
occurs?
public class DrawLine {
public static void main(String[] args) {
[Link]("*****"); // intended four
}
}
(A) No issue; prints four asterisks
(B) Syntax error; missing semicolon
(C) Logical error; it prints five asterisks
(D) Runtime error; ArithmeticException
5
18. The programmer expects the output to be on one line as ”Hello Java”. What is actually
printed and why?
public class PrintSect {
public static void main(String[] args) {
[Link]("Hello ");
[Link]("Java");
}
}
(A) Hello Java (correct, one line)
(B) Hello on one line, Java on the next, due to println
(C) JavaHello on one line
(D) Compilation error: print/println cannot be mixed
19. What is the output?
public class ConcatDemo {
public static void main(String[] args) {
[Link]("Result: " + 1 + 2 + 3);
}
}
(A) Result: 12
(B) Result: 3
(C) Result: 123
(D) Result: 15
20. Which output results when parentheses are used to sum first?
public class ConcatDemo {
public static void main(String[] args) {
[Link]("Result: " + (1 + 2 + 3));
}
}
(A) Result: 123
(B) Result: 6
(C) Result: 1 2 3
(D) Result: 33