250+ TOP MCQs on Creating Threads and Answers

Java MCQs on creating threads in Java Programming Language.

1. Which of these keywords are used to implement synchronization?
a) synchronize
b) syn
c) synch
d) synchronized

Answer: d
Clarification: None.

2. Which of this method is used to avoid polling in Java?
a) wait()
b) notify()
c) notifyAll()
d) all of the mentioned

Answer: d
Clarification: Polling is a usually implemented by looping in CPU is wastes CPU time, one thread being executed depends on other thread output and the other thread depends on the response on the data given to the first thread. In such situation CPU time is wasted, in Java this is avoided by using methods wait(), notify() and notifyAll().

3. Which of these method is used to tell the calling thread to give up a monitor and go to sleep until some other thread enters the same monitor?
a) wait()
b) notify()
c) notifyAll()
d) sleep()

Answer: a
Clarification: wait() method is used to tell the calling thread to give up a monitor and go to sleep until some other thread enters the same monitor. This helps in avoiding polling and minimizes CPU idle time.

4. Which of these method wakes up the first thread that called wait()?
a) wake()
b) notify()
c) start()
d) notifyAll()

Answer: b
Clarification: None.

5. Which of these method wakes up all the threads?
a) wakeAll()
b) notify()
c) start()
d) notifyAll()

Answer: d
Clarification: notifyAll() wakes up all the threads that called wait() on the same object. The highest priority thread will run first.

6. What is synchronization in reference to a thread?
a) It’s a process of handling situations when two or more threads need access to a shared resource
b) It’s a process by which many thread are able to access same shared resource simultaneously
c) It’s a process by which a method is able to access many different threads simultaneously
d) It’s a method that allow too many threads to access any information the require

Answer: a
Clarification: When two or more threads need to access the same shared resource, they need some way to ensure that the resource will be used by only one thread at a time, the process by which this is achieved is called synchronization

7. What will be the output of the following Java program?

  1.     class newthread extends Thread
  2.     {
  3. 	Thread t;
  4. 	String name;
  5. 	newthread(String threadname)
  6.         {
  7. 	    name = threadname;
  8. 	    t = new Thread(this,name);
  9. 	    t.start();
  10. 	}
  11. 	public void run()
  12.         {
  13.         }
  14.  
  15.     }
  16.     class multithreaded_programing
  17.     {
  18.         public static void main(String args[])
  19.         {
  20. 	    newthread obj1 = 	 new newthread("one");
  21. 	    newthread obj2 =	 new newthread("two");
  22.             try
  23.             {
  24.                 obj1.t.wait();	
  25.                 System.out.print(obj1.t.isAlive());
  26.             }
  27.             catch(Exception e)
  28.             {
  29. 	    System.out.print("Main thread interrupted");
  30.             }
  31.         }
  32.     }

a) true
b) false
c) Main thread interrupted
d) None of the mentioned

Answer: c
Clarification: obj1.t.wait() causes main thread to go out of processing in sleep state hence causes exception and “Main thread interrupted” is printed.
Output:

$ javac multithreaded_programing.java
$ java multithreaded_programing
Main thread interrupted

8. What will be the output of the following Java program?

  1.     class newthread extends Thread
  2.     {
  3. 	Thread t;
  4. 	String name;
  5. 	newthread(String threadname)
  6.         {
  7. 	    name = threadname;
  8. 	    t = new Thread(this,name);
  9. 	    t.start();
  10. 	}
  11. 	public void run()
  12.         {
  13.         }
  14.  
  15.     }
  16.     class multithreaded_programing
  17.     {
  18.         public static void main(String args[])
  19.         {
  20. 	    newthread obj1 = 	 new newthread("one");
  21. 	    newthread obj2 =	 new newthread("two");
  22.             try
  23.             {
  24.                 Thread.sleep(1000);	
  25.                 System.out.print(obj1.t.isAlive());
  26.             }
  27.             catch(InterruptedException e)
  28.             {
  29. 	    System.out.print("Main thread interrupted");
  30.             }
  31.         }
  32.     }

a) true
b) false
c) Main thread interrupted
d) None of the mentioned

Answer: b
Clarification: Thread.sleep(1000) has caused all the threads to be suspended for some time, hence onj1.t.isAlive() returns false.
Output:

$ javac multithreaded_programing.java
$ java multithreaded_programing
false

9. What will be the output of the following Java program?

  1.     class newthread extends Thread
  2.     {
  3. 	Thread t;
  4. 	String name;
  5. 	newthread(String threadname)
  6.         {
  7. 	    name = threadname;
  8. 	    t = new Thread(this,name);
  9. 	    t.start();
  10. 	}
  11. 	public void run()
  12.         {
  13.         }
  14.  
  15.     }
  16.     class multithreaded_programing
  17.     {
  18.         public static void main(String args[])
  19.         {
  20. 	    newthread obj1 = 	 new newthread("one");
  21. 	    newthread obj2 =	 new newthread("two");
  22.             try
  23.             {
  24.                  System.out.print(obj1.t.equals(obj2.t));
  25.             }
  26.             catch(Exception e)
  27.             {
  28. 	    System.out.print("Main thread interrupted");
  29.             }
  30.         }
  31.     }

a) true
b) false
c) Main thread interrupted
d) None of the mentioned

Answer: b
Clarification: Both obj1 and obj2 have threads with different name that is “one” and “two” hence obj1.t.equals(obj2.t) returns false.
Output:

$ javac multithreaded_programing.java
$ java multithreaded_programing
false

10. What will be the output of the following Java program?

  1.     class newthread extends Thread
  2.     {
  3. 	Thread t;
  4. 	newthread()
  5.         {
  6. 	    t1 = new Thread(this,"Thread_1");
  7. 	    t2 = new Thread(this,"Thread_2");
  8. 	    t1.start();
  9. 	    t2.start();
  10. 	}
  11. 	public void run()
  12.         {
  13. 	    t2.setPriority(Thread.MAX_PRIORITY);	
  14. 	    System.out.print(t1.equals(t2));
  15.         }    
  16.     }
  17.     class multithreaded_programing
  18.     {
  19.         public static void main(String args[])
  20.         {
  21.             new newthread();        
  22.         }
  23.     }

a) true
b) false
c) truetrue
d) falsefalse

Answer: d
Clarification: This program was previously done by using Runnable interface, here we have used Thread class. This shows both the method are equivalent, we can use any of them to create a thread.
Output:

$ javac multithreaded_programing.java
$ java multithreaded_programing
falsefalse

250+ TOP MCQs on Interfaces – 2 and Answers

This set of Java Interview Questions and Answers for Experienced people on “Interfaces – 2”.

1. Which of the following access specifiers can be used for an interface?
a) Protected
b) Private
c) Public
d) Public, protected, private

Answer: a
Clarification: Interface can have either public access specifier or no specifier. The reason is they need to be implemented by other classes.

2. Which of the following is the correct way of implementing an interface A by class B?
a) class B extends A{}
b) class B implements A{}
c) class B imports A{}
d) None of the mentioned

Answer: b
Clarification: Concrete class implements an interface. They can be instantiated.

3. All methods must be implemented of an interface.
a) True
b) False

Answer: a
Clarification: Concrete classes must implement all methods in an interface. Through interface multiple inheritance is possible.

4. What type of variable can be defined in an interface?
a) public static
b) private final
c) public final
d) static final

Answer: d
Clarification: variable defined in an interface is implicitly final and static. They are usually written in capital letters.

5. What does an interface contain?
a) Method definition
b) Method declaration
c) Method declaration and definition
d) Method name

Answer: b
Clarification: Interface contains the only declaration of the method.

6. What type of methods an interface contain by default?
a) abstract
b) static
c) final
d) private

Answer: a
Clarification: By default, interface contains abstract methods. The abstract methods need to be implemented by concrete classes.

7. What will happen if we provide concrete implementation of method in interface?
a) The concrete class implementing that method need not provide implementation of that method
b) Runtime exception is thrown
c) Compilation failure
d) Method not found exception is thrown

Answer: c
Clarification: The methods of interfaces are always abstract. They provide only method definition.

8. What happens when a constructor is defined for an interface?
a) Compilation failure
b) Runtime Exception
c) The interface compiles successfully
d) The implementing class will throw exception

Answer: a
Clarification: Constructor is not provided by interface as objects cannot be instantiated.

9. What happens when we access the same variable defined in two interfaces implemented by the same class?
a) Compilation failure
b) Runtime Exception
c) The JVM is not able to identify the correct variable
d) The interfaceName.variableName needs to be defined

Answer: d
Clarification: The JVM needs to distinctly know which value of variable it needs to use. To avoid confusion to the JVM interfaceName.variableName is mandatory.

10. Can “abstract” keyword be used with constructor, Initialization Block, Instance Initialization and Static Initialization Block.
a) True
b) False

Answer: b
Clarification: No, Constructor, Static Initialization Block, Instance Initialization Block and variables cannot be abstract.

Java for Interviews, here is complete set on Multiple Choice Questions and Answers on Java.

contest

Advanced 250+ TOP MCQs on Debugging in Eclipse and Answers

This set of Advanced Java Multiple Choice Questions & Answers (MCQs) on “Debugging in Eclipse”.

1. Which mode allows us to run program interactively while watching source code and variables during execution?
a) safe mode
b) debug mode
c) successfully run mode
d) exception mode

Answer: b
Clarification: Debug mode allows us to run program interactively while watching source code and variables during execution.

2. How can we move from one desired step to another step?
a) breakpoints
b) System.out.println
c) logger.log
d) logger.error

Answer: a
Clarification: Breakpoints are inserted in code. We can move from one point to another in the execution of a program.

3. Which part stores the program arguments and startup parameters?
a) debug configuration
b) run configuration
c) launch configuration
d) project configuration

Answer: c
Clarification: Launch configuration stores the startup class, program arguments and vm arguments.

4. How to deep dive into the execution of a method from a method call?
a) F3
b) F5
c) F7
d) F8

Answer: b
Clarification: F5 executes currently selected line and goes to the next line in the program. If the selected line is a method call, debugger steps into the associated code.

5. Which key helps to step out of the caller of currently executed method?
a) F3
b) F5
c) F7
d) F8

Answer: c
Clarification: F7 steps out to the caller of the currently executed method. This finishes the execution of the current method and returns to the caller of this method.

6. Which view allows us to delete and deactivate breakpoints and watchpoints?
a) breakpoint view
b) variable view
c) debug view
d) logger view

Answer: a
Clarification: The Breakpoints view allows us to delete and deactivate breakpoints and watchpoints. We can also modify their properties.

7. What is debugging an application which runs on another java virtual machine on another machine?
a) virtual debugging
b) remote debugging
c) machine debugging
d) compiling debugging

Answer: b
Clarification: Remote debugging allows us to debug applications which run on another Java virtual machine or even on another machine. We need to set certain flags while starting the application.

java -Xdebug -Xnoagent 
-Djava.compiler=NONE 
-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005.

8. What happens when the value of variable change?
a) changed value pop on the screen
b) variable changes are printed in logs
c) dump of variable changes are printed on the screen on end of execution
d) variable tab shows variables highlighted when values change

Answer: d
Clarification: When a variable value changes, the value in variable tab is highlighted yellow in eclipse.

9. Which perspective is used to run a program in debug view?
a) java perspective
b) eclipse perspective
c) debug perspective
d) jdbc perspective

Answer: c
Clarification: We can switch from one perspective to another. Debug perspective shows us the breakpoints, variables, etc.

10. How does eclipse provide the capability for debugging browser actions?
a) internal web browser
b) chrome web browser
c) firefox web browser
d) internet explorer browser

Answer: a
Clarification: Eclipse provides internal web browser to debug browser actions.

250+ TOP MCQs on Control Statements – 2 and Answers

This set of Java Multiple Choice Questions & Answers (MCQs) on “Control Statements”.

1. What would be the output of the following code snippet if variable a=10?

  1. if(a<=0)
  2. {
  3.    if(a==0)
  4.    {
  5.      System.out.println("1 ");
  6.    }
  7.    else 
  8.    { 
  9.       System.out.println("2 ");
  10.    }
  11. }
  12. System.out.println("3 ");

a) 1 2
b) 2 3
c) 1 3
d) 3

Answer: d
Clarification: Since the first if condition is not met, control would not go inside if statement and hence only statement after the entire if block will be executed.

2. The while loop repeats a set of code while the condition is not met?
a) True
b) False

Answer: b
Clarification: While loop repeats a set of code only until the condition is met.

3. What is true about a break?
a) Break stops the execution of entire program
b) Break halts the execution and forces the control out of the loop
c) Break forces the control out of the loop and starts the execution of next iteration
d) Break halts the execution of the loop for certain time frame

Answer: b
Clarification: Break halts the execution and forces the control out of the loop.

4. What is true about do statement?
a) do statement executes the code of a loop at least once
b) do statement does not get execute if condition is not matched in the first iteration
c) do statement checks the condition at the beginning of the loop
d) do statement executes the code more than once always

Answer: a
Clarification: Do statement checks the condition at the end of the loop. Hence, code gets executed at least once.

5. Which of the following is used with the switch statement?
a) Continue
b) Exit
c) break
d) do

Answer: c
Clarification: Break is used with a switch statement to shift control out of switch.

6. What is the valid data type for variable “a” to print “Hello World”?

  1. switch(a)
  2. {
  3.    System.out.println("Hello World");
  4. }

a) int and float
b) byte and short
c) char and long
d) byte and char

Answer: d
Clarification: The switch condition would only meet if variable “a” is of type byte or char.

7. Which of the following is not a decision making statement?
a) if
b) if-else
c) switch
d) do-while

Answer: d
Clarification: do-while is an iteration statement. Others are decision making statements.

8. Which of the following is not a valid jump statement?
a) break
b) goto
c) continue
d) return

Answer: b
Clarification: break, continue and return transfer control to another part of the program and returns back to caller after execution. However, goto is marked as not used in Java.

9. From where break statement causes an exit?
a) Only from innermost loop
b) Terminates a program
c) Only from innermost switch
d) From innermost loops or switches

Answer: d
Clarification: The break statement causes an exit from innermost loop or switch.

10. Which of the following is not a valid flow control statement?
a) exit()
b) break
c) continue
d) return

Answer: a
Clarification: exit() is not a flow control statement in Java. exit() terminates the currently running JVM.

250+ TOP MCQs on Recursion and Answers

Java MCQs on recursion of Java Programming Language.

1. What is Recursion in Java?
a) Recursion is a class
b) Recursion is a process of defining a method that calls other methods repeatedly
c) Recursion is a process of defining a method that calls itself repeatedly
d) Recursion is a process of defining a method that calls other methods which in turn call again this method

Answer: b
Clarification: Recursion is the process of defining something in terms of itself. It allows us to define a method that calls itself.

2. Which of these data types is used by operating system to manage the Recursion in Java?
a) Array
b) Stack
c) Queue
d) Tree

Answer: b
Clarification: Recursions are always managed by using stack.

3. Which of these will happen if recursive method does not have a base case?
a) An infinite loop occurs
b) System stops the program after some time
c) After 1000000 calls it will be automatically stopped
d) None of the mentioned

Answer: a
Clarification: If a recursive method does not have a base case then an infinite loop occurs which results in Stack Overflow.

4. Which of these is not a correct statement?
a) A recursive method must have a base case
b) Recursion always uses stack
c) Recursive methods are faster that programmers written loop to call the function repeatedly using a stack
d) Recursion is managed by Java Runtime environment

Answer: d
Clarification: Recursion is always managed by operating system.

5. Which of these packages contains the exception Stack Overflow in Java?
a) java.lang
b) java.util
c) java.io
d) java.system

Answer: a
Clarification: None.

6. What will be the output of the following Java program?

  1.     class recursion 
  2.     {
  3.         int func (int n) 
  4.         {
  5.             int result;
  6.             result = func (n - 1);
  7.             return result;
  8.         }
  9.     } 
  10.     class Output 
  11.     {
  12.         public static void main(String args[]) 
  13.         {
  14.             recursion obj = new recursion() ;
  15.             System.out.print(obj.func(12));
  16.         }
  17.     }

a) 0
b) 1
c) Compilation Error
d) Runtime Error

Answer: d
Clarification: Since the base case of the recursive function func() is not defined hence infinite loop occurs and results in Stack Overflow.
Output:

$ javac Output.javac
$ java Output
Exception in thread "main" java.lang.StackOverflowError

7. What will be the output of the following Java program?

  1.     class recursion 
  2.     {
  3.         int func (int n) 
  4.         {
  5.             int result;
  6.             if (n == 1)
  7.                 return 1;
  8.             result = func (n - 1);
  9.             return result;
  10.         }
  11.     } 
  12.     class Output 
  13.     {
  14.         public static void main(String args[]) 
  15.         {
  16.             recursion obj = new recursion() ;
  17.             System.out.print(obj.func(5));
  18.         }
  19.     }

a) 0
b) 1
c) 120
d) None of the mentioned

Answer: b
Clarification: None.
Output:

$ javac Output.javac
$ java Output
1

8. What will be the output of the following Java program?

  1.     class recursion 
  2.     {
  3.         int fact(int n) 
  4.         {
  5.             int result;
  6.             if (n == 1)
  7.                 return 1;
  8.             result = fact(n - 1) * n;
  9.             return result;
  10.         }
  11.     } 
  12.     class Output 
  13.     {
  14.         public static void main(String args[]) 
  15.         {
  16.             recursion obj = new recursion() ;
  17.             System.out.print(obj.fact(5));
  18.         }
  19.     }

a) 24
b) 30
c) 120
d) 720

Answer: c
Clarification: fact() method recursively calculates factorial of a number, when value of n reaches 1, base case is excuted and 1 is returned.
Output:

$ javac Output.javac
$ java Output
120

9. What will be the output of the following Java program?

  1.     class recursion 
  2.     {
  3.         int fact(int n) 
  4.         {
  5.             int result;
  6.             if (n == 1)
  7.                 return 1;
  8.             result = fact(n - 1) * n;
  9.             return result;
  10.         }
  11.     } 
  12.     class Output 
  13.     {
  14.         public static void main(String args[]) 
  15.         {
  16.             recursion obj = new recursion() ;
  17.             System.out.print(obj.fact(1));
  18.         }
  19.     }

a) 1
b) 30
c) 120
d) Runtime Error

Answer: a
Clarification: fact() method recursively calculates factorial of a number, when value of n reaches 1, base case is excuted and 1 is returned.
Output:

$ javac Output.javac
$ java Output
1

10. What will be the output of the following Java program?

  1.     class recursion 
  2.     {
  3.         int fact(int n) 
  4.         {
  5.             int result;
  6.             if (n == 1)
  7.                 return 1;
  8.             result = fact(n - 1) * n;
  9.             return result;
  10.         }
  11.     } 
  12.     class Output 
  13.     {
  14.         public static void main(String args[]) 
  15.         {
  16.             recursion obj = new recursion() ;
  17.             System.out.print(obj.fact(6));
  18.         }
  19.     }

a) 1
b) 30
c) 120
d) 720

Answer: d
Clarification: None.
Output:

$ javac Output.javac
$ java Output
720

250+ TOP MCQs on Java.lang – Double & Float Wrappers and Answers

This set of Java Multiple Choice Questions & Answers (MCQs) on “Double & Float Wrappers”.

1. Which of these is a super class of wrappers Double and Float?
a) Long
b) Digits
c) Float
d) Number

Answer: d
Clarification: Number is an abstract class containing subclasses Double, Float, Byte, Short, Integer and Long.

2. Which of the following methods return the value as a double?
a) doubleValue()
b) converDouble()
c) getDouble()
d) getDoubleValue()

Answer: a
Clarification: None.

3. Which of these methods can be used to check whether the given value is a number or not?
a) isNaN()
b) isNumber()
c) checkNaN()
d) checkNumber()

Answer: a
Clarification: isNaN() methods returns true if num specified is not a number, otherwise it returns false.

4. Which of these method of Double wrapper can be used to check whether a given value is infinite or not?
a) Infinite()
b) isInfinite()
c) checkInfinite()
d) None of the mentioned

Answer: b
Clarification: isInfinite() methods returns true if the specified value is an infinite value otherwise it returns false.

5. Which of these exceptions is thrown by compareTo() method defined in a double wrapper?
a) IOException
b) SystemException
c) CastException
d) ClassCastException

Answer: d
Clarification: compareTo() methods compare the specified object to be double, if it is not then ClassCastException is thrown.

6. What will be the output of the following Java code?

  1.     class Output 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Double i = new Double(257.5);  
  6.             boolean x = i.isNaN();
  7.             System.out.print(x);
  8.         }
  9.     }

a) true
b) false
c) 0
d) 1

Answer: b
Clarification: i.isNaN() method returns returns true if i is not a number and false when i is a number. Here false is returned because i is a number i:e 257.5.
Output:

$ javac Output.java
$ java Output
false

7. What will be the output of the following Java code?

  1.     class Output
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Double i = new Double(257.578);  
  6.             int x = i.intValue();
  7.             System.out.print(x);
  8.         }
  9.     }

a) 0
b) 1
c) 256
d) 257

Answer: d
Clarification: i.intValue() method returns the value of wrapper i as a Integer. i is 257.578 is double number when converted to an integer data type its value is 257.
Output:

$ javac Output.java
$ java Output
257

8. What will be the output of the following Java code?

  1.     class Output
  2.     {
  3.         public static void main(String args[])
  4.         {
  5. 	    Double i = new Double(257.578123456789);  
  6.             float x = i.floatValue();
  7.             System.out.print(x);
  8.         }
  9.     }

a) 0
b) 257.0
c) 257.57812
d) 257.578123456789

Answer: c
Clarification: floatValue() converts the value of wrapper i into float, since float can measure till 5 places after decimal hence 257.57812 is stored in floating point variable x.
Output:

$ javac Output.java
$ java Output
257.57812

9. What will be the output of the following Java code?

  1.     class Output
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Double y = new Double(257.57812);
  6. 	    Double i = new Double(257.578123456789);  
  7.             try
  8.             {
  9. 	        int x = i.compareTo(y);
  10.                 System.out.print(x);
  11.             }
  12.             catch(ClassCastException e)
  13.             {
  14.                 System.out.print("Exception");
  15.             }
  16. 	}
  17.     }

a) 0
b) 1
c) Exception
d) None of the mentioned

Answer: b
Clarification: i.compareTo() methods two double values, if they are equal then 0 is returned and if not equal then 1 is returned, here 257.57812 and 257.578123456789 are not equal hence 1 is returned and stored in x.
Output:

$ javac Output.java
$ java Output
1