QUIZ: Topic 1 - Java Basics Total Questions: 10 Instructions: Choose the most correct option (A, B, C or D) for each question. Check your answers using the Answer Key at the end. 1. Which component of Java interprets Java bytecode and makes Java platform independent? A) JRE (Java Runtime Environment) B) JVM (Java Virtual Machine) C) JDK (Java Development Kit) D) The Java compiler 2. Which of the following is a valid Java keyword? A) main B) print C) static D) define 3. A class has instance variables int k; char c1; and String s1; declared but not assigned. What default values do they hold? A) 0, the character with code 0, null B) 0, a blank space character, null C) null, the character with code 0, null D) 0, the character with code 0, the text "null" 4. What are the values of x and y after the statements double y = 19.7; int x = (int) y; are executed? A) x is 19 and y is 19.7 B) x is 20 and y is 19.7 C) x is 19 and y is 19 D) x is 20 and y is 19 5. What is printed by the statements System.out.println(7 / 2); System.out.println((double) 7 / 2); ? A) 3 followed by 3.5 B) 3.5 followed by 3.5 C) 3 followed by 3 D) 4 followed by 3.5 6. Consider the code: int number = 0; int sum = 10; if (number != 0 && sum / number > 5) System.out.println("yes"); else System.out.println("no"); What happens when it runs? A) It prints yes, because sum / number is greater than 5 B) It throws a divide-by-zero runtime error C) It prints no, because && stops evaluating once its left operand is false D) It prints no, because the right operand is evaluated first and 10 / 0 gives 0 7. What is printed by the code: int x = 2; switch (x) { case 1: System.out.println("one"); case 2: System.out.println("two"); case 3: System.out.println("three"); break; default: System.out.println("other"); } A) two B) two and three C) one, two and three D) three 8. What is printed by the code: int x = 1; do { System.out.print(x + " "); x = x + 2; } while (x <= 5); A) 1 3 5 B) 1 3 C) 1 3 5 7 D) 1 2 3 4 5 9. What is printed by the code: int[] list = {3, 5, 7, 9}; int sum = 0; for (int i = 1; i < list.length; i++) sum = sum + list[i]; System.out.println(sum); A) 21 B) 24 C) 20 D) 3 10. For the declaration int[][] matrix = new int[3][4]; which statement is correct? A) matrix.length is 4 B) matrix[0].length is 3 C) matrix.length is 3 and matrix[0].length is 4 D) The array has 4 rows and 3 columns ANSWER KEY 1. B) The JVM interprets Java bytecode, which is what allows the same compiled program to run on any platform. 2. C) static is a reserved keyword; main, print and define are ordinary names and are not keywords in Java. 3. A) Instance variables get 0 for int, the zero character for char, and null for object references such as String. 4. A) Narrowing a double to int with an explicit cast truncates the fractional part, so x becomes 19 while y keeps 19.7. 5. A) Both operands of 7 / 2 are integers so the result is 3, but casting one operand to double makes the result 3.5. 6. C) The && operator short-circuits: since number != 0 is false, the division is never evaluated and the else branch prints no. 7. B) Without a break the case falls through, so case 2 prints two and execution continues into case 3, which prints three before breaking. 8. A) The do-while body runs for x values 1, 3 and 5, printing 1 3 5, and then stops because x becomes 7. 9. A) The loop starts at index 1, so it adds 5 + 7 + 9 = 21 and skips the first element 3. 10. C) matrix.length gives the number of rows (3) and matrix[0].length gives the number of columns (4).