-
Which tool of the JDK turns Hello.java into Hello.class?
- java
- javac
- javadoc
- jar
Show answer
Answer: B · javac
javac is the compiler. The java command starts the JVM to run the compiled class, and jar packs classes into an archive.
-
Which of these is NOT a primitive type in Java?
- boolean
- char
- String
- double
Show answer
Answer: C · String
String is a class, so its values are objects. The other three are primitive types.
-
What is the index of the first element of a Java array?
- 0
- 1
- -1
- It depends on the length of the array
Show answer
Answer: A · 0
Java arrays are zero-based: the first element has index 0 and the last has index length - 1.
-
A program calls sc.nextInt() and then sc.nextLine(), but nextLine() returns an empty string. Why?
- nextLine() only works with text files
- nextInt() closes the Scanner
- The user typed the number wrongly
- nextInt() leaves the end-of-line character in the input, and nextLine() reads up to it
Show answer
Answer: D · nextInt() leaves the end-of-line character in the input, and nextLine() reads up to it
The number is read but the Enter key press stays in the input. The next call to nextLine() consumes that leftover and returns an empty string.
-
What is the value of x? int x = 5; x += 3; x *= 2;
- 13
- 11
- 16
- 10
Show answer
Answer: C · 16
First x becomes 5 + 3 = 8, and then 8 * 2 = 16.
-
Which pair of methods in the same class is an example of overloading?
- int add(int a, int b) and double add(double a, double b)
- int add(int a, int b) and int add(int x, int y)
- int add(int a, int b) in a parent class and the same method in a child class
- int add(int a, int b) and void add(int a, int b)
Show answer
Answer: A · int add(int a, int b) and double add(double a, double b)
Overloading means the same name with different parameter lists. A different return type alone is not enough, and a child class repeating a parent method is overriding.
-
Which keyword makes a field or method belong to the class itself instead of to each object?
- final
- abstract
- private
- static
Show answer
Answer: D · static
A static member is shared by the whole class and can be used through the class name without creating an object.
-
Which statement about inheritance in Java is true?
- A class can extend two classes at the same time
- A class can extend one class and implement several interfaces
- An interface can be instantiated with new
- An abstract class must have no methods with bodies
Show answer
Answer: B · A class can extend one class and implement several interfaces
Java allows a single parent class but any number of interfaces. Interfaces cannot be instantiated, and abstract classes may contain ordinary methods.
-
What does this print? ArrayList<Integer> list = new ArrayList<>(); list.add(10); list.add(20); list.add(30); list.remove(1); System.out.println(list);
- [10, 20, 30]
- [20, 30]
- [10, 30]
- [10, 20]
Show answer
Answer: C · [10, 30]
Calling remove with an int argument removes the element at that index. Index 1 holds 20, so the list becomes [10, 30].
-
What does this print? HashMap<String, Integer> m = new HashMap<>(); m.put("a", 1); m.put("b", 2); m.put("a", 3); System.out.println(m.size() + " " + m.get("a"));
- 3 1
- 2 3
- 3 3
- 2 1
Show answer
Answer: B · 2 3
A key can appear only once. The second put with the key "a" replaces the value 1 by 3, so the map has 2 entries and "a" maps to 3.
Want to test yourself? Take the same questions as a timed practice test and see your score. Start the practice test