☕ Java Programming MCQs and Exam Questions

Practice 80 approved ☕ Java Programming MCQs and exam questions from the 💻 Programming subject.

80 solved MCQs

public class MyRunnable implements Runnable { public void run() { // some code here } } which of these will create and start this thread?Answer: new Thread(new MyRunnable()).start(); Which is true about an anonymous inner class?Answer: It can extend exactly one class or implement exactly one interface. class Boo { Boo(String s) { } Boo() { } } class Bar extends Boo { Bar() { } Bar(String s) {super(s);} void zoo() { // insert code here } } class Boo { Boo(String s) { } Boo() { } } class Bar extends Boo { Bar() { } Bar(String s) {super(s);} void zoo() { // insert code here } } which one create an anonymous inner class from within class Bar?Answer: Boo f = new Bar() { }; Which is true about a method-local inner class?Answer: It can be marked abstract. Which statement is true about a static nested class?Answer: It does not have access to nonstatic members of the enclosing class. Which constructs an anonymous inner class instance?Answer: System.out.println(new Runnable() {public void run() { }}); Suppose that you would like to create an instance of a new Map that has an iteration order that is the same as the iteration order of an existing instance of a Map. Which concrete implementation of the Map interface should be used for the new instance?Answer: LinkedHashMap Which class does not override the equals() and hashCode() methods, inheriting them directly from class Object?Answer: java.lang.StringBuffer Which collection class allows you to grow or shrink its size and provides indexed access to its elements, but whose methods are not synchronized?Answer: java.util.ArrayList Which interface does java.util.Hashtable implement?Answer: Java.util.Map Which interface provides the capability to store objects using a key-value pair?Answer: Java.util.Map Which collection class allows you to associate its elements with key values, and allows you to retrieve objects in FIFO (first-in, first-out) sequence?Answer: java.util.LinkedHashMap Which collection class allows you to access its elements by associating a key with an element's value, and provides synchronization?Answer: java.util.Hashtable which is valid declaration of a float?Answer: float f = 1F; What is the numerical range of char?Answer: 0 to 65535 Which of the following are Java reserved words? 1. run 2. import 3. default 4. implementAnswer: 2 and 3 Which three form part of correct array declarations? 1. public int a [ ] 2. static int [ ] a 3. public [ ] int a 4. private int a [3] 5. private int [3] a [ ] 6. public final int [ ] aAnswer: 1, 2, 6 public class Test { } What is the prototype of the default constructor?Answer: public Test( ) What is the most restrictive access modifier that will allow members of one class to have access to members of another class in the same package?Answer: default access Which of the following is/are legal method declarations? 1. protected abstract void m1(); 2. static final void m1(){} 3. synchronized public final void m1() {} 4. private native void m1();Answer: All of them are legal declarations. Which cause a compiler error?Answer: int [ ][ ] scores = {2,7,6}, {9,3,45}; Which three are valid method signatures in an interface? 1. private int getArea(); 2. public float getVol(float x); 3. public void main(String [] args); 4. public static void main(String [] args); 5. boolean setFlag(Boolean [] test);Answer: 2, 3 and 5 You want a class to have access to members of another class in the same package. Which is the most restrictive access that accomplishes this objective?Answer: default access Which one creates an instance of an array?Answer: int[ ] ia = new int[15]; Which two of the following are legal declarations for nonnested classes and interfaces? 1. final abstract class Test {} 2. public static interface Test {} 3. final public class Test {} 4. protected abstract class Test {} 5. protected interface Test {} 6. abstract public class Test {}Answer: 3 and 6 Which of the following class level (nonlocal) variable declarations will not compile?Answer: private synchronized int e; Which two cause a compiler error? 1. float[ ] f = new float(3); 2. float f2[ ] = new float[ ]; 3. float[ ]f1 = new float[3]; 4. float f3[ ] = new float[3]; 5. float f5[ ] = {1.0f, 2.0f, 2.0f};Answer: 1, 2 Given a method in a protected class, what access modifier do you use to restrict access to that method to only the other members of the same class?Answer: private Which is a valid declaration within an interface?Answer: public static short stop = 23; Which four options describe the correct default values for array elements of the types indicated? 1. int -> 0 2. String -> "null" 3. Dog -> null 4. char -> '\u0000' 5. float -> 0.0f 6. boolean -> trueAnswer: 1, 3, 4, 5