Java Quiz for Beginners
Questions: 16 · 10 minutes
1. Which method serves as the usual entry point when a Java application is launched by the JVM?
public void start(String[] args)
public static void main(String[] args)
static int main(String args)
public static void run()
2. In the condition `value != 0 && 10 / value > 2`, why does setting `value` to zero not cause division by zero?
Java automatically changes zero to one during division
Integer division by zero returns zero inside a condition
The `&&` operator evaluates the division before checking `value`
The second expression is skipped when the first expression is false
3. What value is printed after this loop finishes?
int total = 0;
for (int i = 1; i <= 3; i++) {
total += i;
}
System.out.println(total);
6
4
7
3
4. Given this method, what value does the expression `twice(4) + 1` produce?
static int twice(int n) {
return n * 2;
}
8
9
A compile-time error because methods cannot return calculations
10
5. An array is declared as `int[] scores = {10, 20, 30};`. Which value is retrieved by `scores[1]`?
10
20
30
An array index exception is thrown
6. Which keyword declares that one Java class inherits from another class?
instanceof
implements
inherits
extends
7. Which access modifier restricts a field or method to the class in which it is declared?
public
protected
private
static
8. A `switch` starts at `case 2` in the code below. What characters are printed?
int code = 2;
switch (code) {
case 1: System.out.print(""A"");
case 2: System.out.print(""B"");
case 3: System.out.print(""C""); break;
default: System.out.print(""D"");
}
B
BC
ABC
BD
9. What is the main purpose of a `catch` block in Java?
To prevent every exception from occurring
To repeat the associated `try` block until it succeeds
To handle a matching exception thrown while the associated `try` block runs
To declare that a method returns an exception
10. You have declared `ArrayList<String> names = new ArrayList<>();`. Which statement adds `""Maya""` to the list?
names.append(""Maya"");
names.insert(""Maya"");
names.add(""Maya"");
names.push(""Maya"");
11. Which statement correctly describes a Java constructor?
It can run only after an object has already been fully created
It must be named `constructor` and return the new object
It has the same name as its class and does not declare a return type
It must be declared `static` so the class can call it
12. A program evaluates `int result = 7 / 2;` and then prints `result`. Which value appears?
3.5
The code does not compile because division requires double values
4
3
13. Two `String` variables may refer to different objects, but you need to compare the text they contain. Which expression should you use?
Assign one variable to the other with `first = second`
Compare the references with `first == second`
Call a nonexistent `first.compare(second)` method
Compare the contents with `first.equals(second)`
14. After the enhanced `for` loop below processes every array element, what value is stored in `sum`?
int sum = 0;
int[] values = {2, 4, 6};
for (int value : values) {
sum += value;
}
12
10
24
6
15. A local variable is declared and read inside a method as shown below. What happens?
int count;
System.out.println(count);
The code fails to compile because the local variable may not have been initialized
The code prints `null` because `count` has not been assigned
The code prints an unpredictable integer left in memory
The code prints `0` because every numeric variable receives a default value
16. Which Java type is designed to store only `true` or `false`?
boolean
bit
int
String