

MCQOPTIONS
Saved Bookmarks
This section includes 31 Mcqs, each offering curated multiple-choice questions to sharpen your Java Programming knowledge and support exam preparation. Choose a topic below to get started.
1. |
Which of the following statements about the method are incorrect? |
A. | 1 and 2 |
B. | 2 and 3 |
C. | 3 and 4 |
D. | 1 and 4 |
Answer» C. 3 and 4 | |
2. |
What two statements are true about properly overridden and methods? |
A. | 1 and 2 |
B. | 2 and 3 |
C. | 3 and 4 |
D. | 1 and 3 |
Answer» D. 1 and 3 | |
3. |
Which two statements are true about comparing two instances of the same class, given that the and methods have been properly overridden? |
A. | 1 and 4 |
B. | 2 and 3 |
C. | 3 and 4 |
D. | 1 and 3 |
Answer» B. 2 and 3 | |
4. |
and assuming that the and methods are properly implemented, if the output is "", which of the following statements will always be true? |
A. | x2.equals(x1) |
B. | x3.hashCode() == x4.hashCode() |
C. | x5.hashCode() != x6.hashCode() |
D. | x8.equals(x7) |
Answer» C. x5.hashCode() != x6.hashCode() | |
5. |
Which of the following are true statements? |
A. | 2, 3, 4 and 5 |
B. | 1, 3, 4 and 5 |
C. | 3, 4 and 5 |
D. | 1, 2 and 3 |
Answer» C. 3, 4 and 5 | |
6. |
Which statement is true for the class ? |
A. | The elements in the collection are ordered. |
B. | The collection is guaranteed to be immutable. |
C. | The elements in the collection are guaranteed to be unique. |
D. | The elements in the collection are accessed using a unique key. |
Answer» B. The collection is guaranteed to be immutable. | |
7. |
Suppose that you would like to create an instance of a new that has an iteration order that is the same as the iteration order of an existing instance of a . Which concrete implementation of the interface should be used for the new instance? |
A. | TreeMap |
B. | HashMap |
C. | LinkedHashMap |
D. | The answer depends on the implementation of the existing instance. |
Answer» D. The answer depends on the implementation of the existing instance. | |
8. |
Which class does not override the and methods, inheriting them directly from class Object? |
A. | java.lang.String |
B. | java.lang.Double |
C. | java.lang.StringBuffer |
D. | java.lang.Character |
Answer» D. java.lang.Character | |
9. |
Which interface does implement? |
A. | Java.util.Map |
B. | Java.util.List |
C. | Java.util.HashTable |
D. | Java.util.Collection |
Answer» B. Java.util.List | |
10. |
What line of code should replace the missing statement to make this program compile? |
A. | No statement required. |
B. | import java.io.*; |
C. | include java.io.*; |
D. | import java.io.PrintWriter; |
Answer» B. import java.io.*; | |
11. |
What will be the output of the program?_x000D_ public class Test _x000D_ { _x000D_ private static float[] f = new float[2]; _x000D_ public static void main (String[] args) _x000D_ {_x000D_ System.out.println("f[0] = " + f[0]); _x000D_ } _x000D_ } |
A. | f[0] = 0 |
B. | f[0] = 0.0 |
C. | Compile Error |
D. | Runtime Exception |
Answer» C. Compile Error | |
12. |
What will be the output of the program? public class Test { public static void main (String[] args) { String foo = args[1]; String bar = args[2]; String baz = args[3]; System.out.println("baz = " + baz); /* Line 8 */ } } And the command line invocation: > java Test red green blue |
A. | baz = |
B. | baz = null |
C. | baz = blue |
D. | Runtime Exception |
Answer» E. | |
13. |
What will be the output of the program? TreeSet map = new TreeSet(); map.add("one"); map.add("two"); map.add("three"); map.add("four"); map.add("one"); Iterator it = map.iterator(); while (it.hasNext() ) { System.out.print( it.next() + " " ); } |
A. | one two three four |
B. | four three two one |
C. | four one three two |
D. | one two three four one |
Answer» D. one two three four one | |
14. |
What will be the output of the program? import java.util.*; class H { public static void main (String[] args) { Object x = new Vector().elements(); System.out.print((x instanceof Enumeration)+","); System.out.print((x instanceof Iterator)+","); System.out.print(x instanceof ListIterator); } } |
A. | Prints: false,false,false |
B. | Prints: false,false,true |
C. | Prints: false,true,false |
D. | Prints: true,false,false |
Answer» E. | |
15. |
What will be the output of the program? public static void main(String[] args) { Object obj = new Object() { public int hashCode() { return 42; } }; System.out.println(obj.hashCode()); } |
A. | 42 |
B. | Runtime Exception |
C. | Compile Error at line 2 |
D. | Compile Error at line 5 |
Answer» B. Runtime Exception | |
16. |
Which statement is true for the class java.util.ArrayList? |
A. | The elements in the collection are ordered. |
B. | The collection is guaranteed to be immutable. |
C. | The elements in the collection are guaranteed to be unique. |
D. | The elements in the collection are accessed using a unique key. |
Answer» B. The collection is guaranteed to be immutable. | |
17. |
Which of the following are true statements? The Iterator interface declares only three methods: hasNext, next and remove. The ListIterator interface extends both the List and Iterator interfaces. The ListIterator interface provides forward and backward iteration capabilities. The ListIterator interface provides the ability to modify the List during iteration. The ListIterator interface provides the ability to determine its position in the List. |
A. | 2, 3, 4 and 5 |
B. | 1, 3, 4 and 5 |
C. | 3, 4 and 5 |
D. | 1, 2 and 3 |
Answer» C. 3, 4 and 5 | |
18. |
Which two statements are true about comparing two instances of the same class, given that the equals() and hashCode() methods have been properly overridden? If the equals() method returns true, the hashCode() comparison == must return true. If the equals() method returns false, the hashCode() comparison != must return true. If the hashCode() comparison == returns true, the equals() method must return true. If the hashCode() comparison == returns true, the equals() method might return true. |
A. | 1 and 4 |
B. | 2 and 3 |
C. | 3 and 4 |
D. | 1 and 3 |
Answer» B. 2 and 3 | |
19. |
What will be the output of the program? package foo; import java.util.Vector; /* Line 2 */ private class MyVector extends Vector { int i = 1; /* Line 5 */ public MyVector() { i = 2; } } public class MyNewVector extends MyVector { public MyNewVector () { i = 4; /* Line 15 */ } public static void main (String args []) { MyVector v = new MyNewVector(); /* Line 19 */ } } |
A. | Compilation will succeed. |
B. | Compilation will fail at line 3. |
C. | Compilation will fail at line 5. |
D. | Compilation will fail at line 15. |
Answer» C. Compilation will fail at line 5. | |
20. |
What will be the output of the program? public class Test { private static float[] f = new float[2]; public static void main (String[] args) { System.out.println("f[0] = " + f[0]); } } |
A. | f[0] = 0 |
B. | f[0] = 0.0 |
C. | Compile Error |
D. | Runtime Exception |
Answer» C. Compile Error | |
21. |
Which of the following statements about the hashcode() method are incorrect? The value returned by hashcode() is used in some collection classes to help locate objects. The hashcode() method is required to return a positive int value. The hashcode() method in the String class is the one inherited from Object. Two new empty String objects will produce identical hashcodes. |
A. | 1 and 2 |
B. | 2 and 3 |
C. | 3 and 4 |
D. | 1 and 4 |
Answer» C. 3 and 4 | |
22. |
What will be the output of the program? public class Test { public static void main (String args[]) { String str = NULL; System.out.println(str); } } |
A. | NULL |
B. | Compile Error |
C. | Code runs but no output |
D. | Runtime Exception |
Answer» C. Code runs but no output | |
23. |
What will be the output of the program? import java.util.*; class I { public static void main (String[] args) { Object i = new ArrayList().iterator(); System.out.print((i instanceof List)+","); System.out.print((i instanceof Iterator)+","); System.out.print(i instanceof ListIterator); } } |
A. | Prints: false, false, false |
B. | Prints: false, false, true |
C. | Prints: false, true, false |
D. | Prints: false, true, true |
Answer» D. Prints: false, true, true | |
24. |
x = 0; if (x1.hashCode() != x2.hashCode() ) x = x + 1; if (x3.equals(x4) ) x = x + 10; if (!x5.equals(x6) ) x = x + 100; if (x7.hashCode() == x8.hashCode() ) x = x + 1000; System.out.println("x = " + x); and assuming that the equals() and hashCode() methods are properly implemented, if the output is "x = 1111", which of the following statements will always be true? |
A. | x2.equals(x1) |
B. | x3.hashCode() == x4.hashCode() |
C. | x5.hashCode() != x6.hashCode() |
D. | x8.equals(x7) |
Answer» C. x5.hashCode() != x6.hashCode() | |
25. |
Which statement is true for the class java.util.HashSet? |
A. | The elements in the collection are ordered. |
B. | The collection is guaranteed to be immutable. |
C. | The elements in the collection are guaranteed to be unique. |
D. | The elements in the collection are accessed using a unique key. |
Answer» D. The elements in the collection are accessed using a unique key. | |
26. |
/* Missing Statement ? */ public class foo { public static void main(String[]args)throws Exception { java.io.PrintWriter out = new java.io.PrintWriter(); new java.io.OutputStreamWriter(System.out,true); out.println("Hello"); } } What line of code should replace the missing statement to make this program compile? |
A. | No statement required. |
B. | import java.io.*; |
C. | include java.io.*; |
D. | import java.io.PrintWriter; |
Answer» B. import java.io.*; | |
27. |
What will be the output of the program? public class Test { private static int[] x; public static void main(String[] args) { System.out.println(x[0]); } } |
A. | 0  |
B. | null |
C. | Compile Error |
D. | NullPointerException at runtime |
Answer» E. | |
28. |
Which of the following are Java reserved words? run import default implement |
A. | 1 and 2 |
B. | 2 and 3 |
C. | 3 and 4 |
D. | 2 and 4 |
Answer» C. 3 and 4 | |
29. |
What two statements are true about properly overridden hashCode() and equals() methods? hashCode() doesn't have to be overridden if equals() is. equals() doesn't have to be overridden if hashCode() is. hashCode() can always return the same value, regardless of the object that invoked it. equals() can be true even if it's comparing different objects. |
A. | 1 and 2 |
B. | 2 and 3 |
C. | 3 and 4 |
D. | 1 and 3 |
Answer» D. 1 and 3 | |
30. |
What will be the output of the program? public class Test { public static void main (String[] args) { String foo = args[1]; String bar = args[2]; String baz = args[3]; System.out.println("baz = " + baz); /* Line 8 */ } } And the command line invocation: > java Test red green blue |
A. | baz = |
B. | baz = null |
C. | baz = blue |
D. | Runtime Exception |
Answer» E. | |
31. |
class Test1 { public int value; public int hashCode() { return 42; } } class Test2 { public int value; public int hashcode() { return (int)(value^5); } } which statement is true? |
A. | class Test1 will not compile. |
B. | The Test1 hashCode() method is more efficient than the Test2 hashCode() method. |
C. | The Test1 hashCode() method is less efficient than the Test2 hashCode() method. |
D. | class Test2 will not compile. |
Answer» D. class Test2 will not compile. | |