☕ Java Programming MCQs and Exam Questions

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

80 solved MCQs

What is the value of "d" after this line of code has been executed? double d = Math.round ( 2.5 + Math.random() );Answer: 3 Which of the following would compile without error?Answer: int a = Math.abs(-5); Which of the following are valid calls to Math.max? 1. Math.max(1,4) 2. Math.max(2.3, 5) 3. Math.max(1, 3, 5, 7) 4. Math.max(-1.5, -2.8f)Answer: 1, 2 and 4 public class Myfile { public static void main (String[] args) { String biz = args[1]; String baz = args[2]; String rip = args[3]; System.out.println("Arg is " + rip); } } Select how you would start the program to cause it to print: Arg is 2Answer: java Myfile 1 3 2 2 What will be the output of the program? public class Test { public static int y; public static void foo(int x) { System.out.print("foo "); y = x; } public static int bar(int z) { System.out.print("bar "); return y = z; } public static void main(String [] args ) { int t = 0; assert t > 0 : bar(7); assert t > 1 : foo(8); /* Line 18 */ System.out.println("done "); } }Answer: Compilation fails What will be the output of the program (when you run with the -ea option) ? public class Test { public static void main(String[] args) { int x = 0; assert (x > 0) : "assertion failed"; /* Line 6 */ System.out.println("finished"); } }Answer: An AssertionError is thrown. public class Test2 { public static int x; public static int foo(int y) { return y * 2; } public static void main(String [] args) { int z = 5; assert z > 0; /* Line 11 */ assert z > 2: foo(z); /* Line 12 */ if ( z < 7 ) assert z > 4; /* Line 14 */ switch (z) { case 4: System.out.println("4 "); case 5: System.out.println("5 "); default: assert z < 10; } if ( z < 10 ) assert z > 4: z++; /* Line 22 */ System.out.println(z); } } 5. public class Test2 { public static int x; public static int foo(int y) { return y * 2; } public static void main(String [] args) { int z = 5; assert z > 0; /* Line 11 */ assert z > 2: foo(z); /* Line 12 */ if ( z < 7 ) assert z > 4; /* Line 14 */ switch (z) { case 4: System.out.println("4 "); case 5: System.out.println("5 "); default: assert z < 10; } if ( z < 10 ) assert z > 4: z++; /* Line 22 */ System.out.println(z); } } which line is an example of an inappropriate use of assertions?Answer: Line 22 void start() { A a = new A(); B b = new B(); a.s(b); b = null; /* Line 5 */ a = null; /* Line 6 */ System.out.println("start completed"); /* Line 7 */ } When is the B object, created in line 3, eligible for garbage collection?Answer: There is no way to be absolutely certain. class HappyGarbage01 { public static void main(String args[]) { HappyGarbage01 h = new HappyGarbage01(); h.methodA(); /* Line 6 */ } Object methodA() { Object obj1 = new Object(); Object [] obj2 = new Object[1]; obj2[0] = obj1; obj1 = null; return obj2[0]; } } Where will be the most chance of the garbage collector being invoked?Answer: Garbage collector never invoked in methodA() class Bar { } class Test { Bar doBar() { Bar b = new Bar(); /* Line 6 */ return b; /* Line 7 */ } public static void main (String args[]) { Test t = new Test(); /* Line 11 */ Bar newBar = t.doBar(); /* Line 12 */ System.out.println("newBar"); newBar = new Bar(); /* Line 14 */ System.out.println("finishing"); /* Line 15 */ } } At what point is the Bar object, created on line 6, eligible for garbage collection?Answer: after line 14 class Test { private Demo d; void start() { d = new Demo(); this.takeDemo(d); /* Line 7 */ } /* Line 8 */ void takeDemo(Demo demo) { demo = null; demo = new Demo(); } } When is the Demo object eligible for garbage collection?Answer: When the instance running this code is made eligible for garbage collection. public class X { public static void main(String [] args) { X x = new X(); X x2 = m1(x); /* Line 6 */ X x4 = new X(); x2 = x4; /* Line 8 */ doComplexStuff(); } static X m1(X mx) { mx = new X(); return mx; } } After line 8 runs. how many objects are eligible for garbage collection?Answer: 1 public Object m() { Object o = new Float(3.14F); Object [] oa = new Object[l]; oa[0] = o; /* Line 5 */ o = null; /* Line 6 */ oa[0] = null; /* Line 7 */ return o; /* Line 8 */ } When is the Float object, created in line 3, eligible for garbage collection?Answer: just after line 7 class X2 { public X2 x; public static void main(String [] args) { X2 x2 = new X2(); /* Line 6 */ X2 x3 = new X2(); /* Line 7 */ x2.x = x3; x3.x = x2; x2 = new X2(); x3 = x2; /* Line 11 */ doComplexStuff(); } } after line 11 runs, how many objects are eligible for garbage collection?Answer: 2 What is the name of the method used to start a thread execution?Answer: start(); Which two are valid constructors for Thread? 1. Thread(Runnable r, String name) 2. Thread() 3. Thread(int priority) 4. Thread(Runnable r, ThreadGroup g) 5. Thread(Runnable r, int priority)Answer: 1 and 2 Which three are methods of the Object class? 1. notify(); 2. notifyAll(); 3. isInterrupted(); 4. synchronized(); 5. interrupt(); 6. wait(long msecs); 7. sleep(long msecs); 8. yield();Answer: 1, 2, 6 class X implements Runnable { public static void main(String args[]) { /* Missing code? */ } public void run() {} } Which of the following line of code is suitable to start a thread ?Answer: X run = new X(); Thread t = new Thread(run); t.start(); Which cannot directly cause a thread to stop executing?Answer: Calling notify() method on an object. Which two of the following methods are defined in class Thread? 1. start() 2. wait() 3. notify() 4. run() 5. terminate()Answer: 1 and 4 Which three guarantee that a thread will leave the running state? 1. yield() 2. wait() 3. notify() 4. notifyAll() 5. sleep(1000) 6. aLiveThread.join() 7. Thread.killThread()Answer: 2, 5 and 6 Which of the following will directly stop the execution of a Thread?Answer: wait() Which method must be defined by a class implementing the java.lang.Runnable interface?Answer: public void run() Which will contain the body of the thread?Answer: run(); Which method registers a thread in a thread scheduler?Answer: start(); Assume the following method is properly synchronized and called from a thread A on an object B: wait(2000); After calling this method, when will the thread A become a candidate to get another turn at the CPU?Answer: After thread A is notified, or after two seconds. Which of the following will not directly cause a thread to stop?Answer: notify() Which of the following will not directly cause a thread to stop?Answer: notify() Which class or interface defines the wait(), notify(),and notifyAll() methods?Answer: Object 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();