250+ TOP MCQs on Thread class and Answers

Java MCQs on Thread class of Java Programming Language.

1. Which of these method of Thread class is used to find out the priority given to a thread?
a) get()
b) ThreadPriority()
c) getPriority()
d) getThreadPriority()

Answer: c
Clarification: None.

2. Which of these method of Thread class is used to Suspend a thread for a period of time?
a) sleep()
b) terminate()
c) suspend()
d) stop()

Answer: a
Clarification: None.

3. Which function of pre defined class Thread is used to check weather current thread being checked is still running?
a) isAlive()
b) Join()
c) isRunning()
d) Alive()

Answer: a
Clarification:isAlive() function is defined in class Thread, it is used for implementing multithreading and to check whether the thread called upon is still running or not.

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

  1.     class multithreaded_programing
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Thread t = Thread.currentThread();
  6.             t.setName("New Thread");
  7.             System.out.println(t);        
  8.         }
  9.     }

a) Thread[5,main]
b) Thread[New Thread,5]
c) Thread[main,5,main]
d) Thread[New Thread,5,main]

Answer: d
Clarification: None.
Output:

$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[New Thread,5,main]

5. What is the priority of the thread in output in the following Java program?

  1.     class multithreaded_programing
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Thread t = Thread.currentThread();
  6.             t.setName("New Thread");
  7.             System.out.println(t.getName());        
  8.         }
  9.     }

a) main
b) Thread
c) New Thread
d) Thread[New Thread,5,main]

Answer: c
Clarification: The getName() function is used to obtain the name of the thread, in this code the name given to thread is ‘New Thread’.
Output:

$ javac multithreaded_programing.java
$ java multithreaded_programing
New Thread

6. What is the name of the thread in output in the following Java program?

  1.     class multithreaded_programing
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Thread t = Thread.currentThread();
  6.             System.out.println(t.getPriority());        
  7.         }
  8.     }

a) 0
b) 1
c) 4
d) 5

Answer: d
Clarification: The default priority given to a thread is 5.
Output:

$ javac multithreaded_programing.java
$ java multithreaded_programing
5

7. What is the name of the thread in output in the following Java program?

  1.     class multithreaded_programing
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Thread t = Thread.currentThread();
  6.             System.out.println(t.isAlive());        
  7.         }
  8.     }

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

Answer: c
Clarification: Thread t is seeded to currently program, hence when you run the program the thread becomes active & code ‘t.isAlive’ returns true.
Output:

$ javac multithreaded_programing.java
$ java multithreaded_programing
true

250+ TOP MCQs on Observable & Timer Class and Answers

Java MCQs on Observable & Timer class of Java Programming Language.

1. What is the use of Observable class?
a) It is used to create global subclasses
b) It is used to create classes that other part of the program can observe
c) It is used to create classes that can be accessed by other parts of program
d) It is used to create methods that can be accessed by other parts of program

Answer: b
Clarification: The Observable class is used to create subclasses that other part of program can observe.

2. Which of these methods is used to notify observer the change in observed object?
a) update()
b) notify()
c) check()
d) observed()

Answer: a
Clarification: None.

3. Which of these methods calls update() method?
a) notify()
b) observeObject()
c) updateObserver()
d) notifyObserver()

Answer: d
Clarification: notifyObserver() notifies all the observers of the invoking object that it has changed by calling update(). A null is passed as the second argument to update().

4. Which of these methods is called when observed object has changed?
a) setChanged()
b) update()
c) notifyObserver()
d) all of the mentioned

Answer: d
Clarification: None.

5. Which of these classes can schedule task for execution in future?
a) Thread
b) Timer
c) System
d) Observer

Answer: b
Clarification: Timer and TimerTask are the classes that support the ability to schedule tasks for execution at some future time.

6. Which of these interfaces is implemented by TimerTask class?
a) Runnable
b) Thread
c) Observer
d) ThreadCount

Answer: a
Clarification: None.

7. Which of these package provides the ability to read and write in Zip format?
a) java.lang
b) java.io
c) java.util.zip
d) java.util.zar

Answer: c
Clarification: None.

Global Education & Learning Series – Java Programming Language.

Advanced 250+ TOP MCQs on Java Beans and Answers

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

1. Which of the following is not an Enterprise Beans type?
a) Doubleton
b) Singleton
c) Stateful
d) Stateless

Answer: a
Clarification: Stateful, Stateless and Singleton are session beans.

2. Which of the following is not true about Java beans?
a) Implements java.io.Serializable interface
b) Extends java.io.Serializable class
c) Provides no argument constructor
d) Provides setter and getter methods for its properties

Answer: b
Clarification: java.io.Serializable is not a class. Instead it is an interface. Hence it cannot be extended.

3. Which file separator should be used by MANIFEST file?
a) /
b)
c) –
d) //

Answer: a
Clarification: MANIFEST file uses classes using / file separator.

4. Which of the following is correct error when loading JAR file with duplicate name?
a) java.io.NullPointerException
b) java.lang.ClassNotFound
c) java.lang.ClassFormatError
d) java.lang.DuplicateClassError

Answer: c
Clarification: java.lang.ClassFormatError: Duplicate Name error is thrown when .class file in the JAR contains a class whose class name is different from the expected name.

5. Java Beans are extremely secured?
a) True
b) False

Answer: b
Clarification: JavaBeans do not add any security features to the Java platform.

6. Which of the following is not a feature of Beans?
a) Introspection
b) Events
c) Persistence
d) Serialization

Answer: d
Clarification: Serialization is not the feature of Java Beans. Introspection, Customization, Events, Properties and Persistence are the features.

7. What is the attribute of java bean to specify scope of bean to have single instance per Spring IOC?
a) prototype
b) singleton
c) request
d) session

Answer: b
Clarification: Singleton scope of bean specifies only one instance per spring IOC container. This is the default scope.

8. Which attribute is used to specify initialization method?
a) init
b) init-method
c) initialization
d) initialization-method

Answer: b
Clarification: init-method is used to specify the initialization method.

 <bean id = "helloWorld" class = "com.bean.HelloWorld" init-method = "init" />

9. Which attribute is used to specify destroy method?
a) destroy
b) destroy-method
c) destruction
d) destruction-method

Answer: b
Clarification: destroy-method is used to specify the destruction method.

 <bean id = "helloWorld" class = "com.tutorialspoint.HelloWorld" destroy-method = "destroy" />

10. How to specify autowiring by name?
a) @Qualifier
b) @Type
c) @Constructor
d) @Name

Answer: a
Clarification: Different beans of the same class are identified by name.

  1. 	     @Qualifier("student1")
  2. 	     @Autowired
  3. 	     Student student1;

250+ TOP MCQs on Relational Operators and Boolean Logic Operators and Answers

Java MCQs on relational operators and boolean logic operators of Java Programming Language.

1. What is the output of relational operators?
a) Integer
b) Boolean
c) Characters
d) Double

Answer: b
Clarification: None.

2. Which of these is returned by “greater than”, “less than” and “equal to” operators?
a) Integers
b) Floating – point numbers
c) Boolean
d) None of the mentioned

Answer: c
Clarification: All relational operators return a boolean value ie. true and false.

3. Which of the following operators can operate on a boolean variable?

a) 3 & 2
b) 1 & 4
c) 1, 2 & 4
d) 1, 2 & 3

Answer: d
Clarification: Operator Short circuit AND, &&, equal to, == , ternary if-then-else, ?:, are boolean logical operators. += is an arithmetic operator it can operate only on numeric values.

4. Which of these operators can skip evaluating right hand operand?
a) !
b) |
c) &
d) &&

Answer: d
Clarification: Operator short circuit and, &&, and short circuit or, ||, skip evaluating right hand operand when output can be determined by left operand alone.

5. Which of these statements is correct?
a) true and false are numeric values 1 and 0
b) true and false are numeric values 0 and 1
c) true is any non zero value and false is 0
d) true and false are non numeric values

Answer: d
Clarification: True and false are keywords, they are non numeric values which do not relate to zero or non zero numbers. true and false are boolean values.

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

  1.     class Relational_operator 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             int var1 = 5; 
  6.             int var2 = 6;
  7.             System.out.print(var1 > var2);
  8.         } 
  9.     }

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

Answer: d
Clarification: Operator > returns a boolean value. 5 is not greater than 6 therefore false is returned.
output:

$ javac Relational_operator.java
$ java Relational_operator
false

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

  1.     class bool_operator 
  2.     {
  3.         public static void main(String args[]) 
  4.         {    
  5.              boolean a = true;
  6.              boolean b = !true;
  7.              boolean c = a | b;
  8.  	     boolean d = a & b;
  9.              boolean e = d ? b : c;
  10.              System.out.println(d + " " + e);
  11.         } 
  12.     }

a) false false
b) true ture
c) true false
d) false true

Answer: d
Clarification: Operator | returns true if any one operand is true, thus ‘c = true | false’ is true. Operator & returns a true if both of the operand is true thus d is false. Ternary operator ?: assigns left of ‘:’ if condition is true and right hand of ‘:’ if condition is false. d is false thus e = d ? b : c , assigns c to e , e contains true.
output:

$ javac bool_operator.java
$ java bool_operator
false true

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

  1.     class ternary_operator 
  2.     {
  3.         public static void main(String args[]) 
  4.         {        
  5.              int x = 3;
  6.              int y = ~ x;
  7.              int z;
  8.              z = x > y ? x : y;
  9.              System.out.print(z);
  10.         } 
  11.     }

a) 0
b) 1
c) 3
d) -4

Answer: c
Clarification: None.
output:

$ javac ternary_operator.java
$ java ternary_operator
3

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

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {    
  5.              int x , y = 1;
  6.              x = 10;
  7.              if (x != 10 && x / 0 == 0)
  8.                  System.out.println(y);
  9.              else
  10.                  System.out.println(++y);
  11.         } 
  12.     }

a) 1
b) 2
c) Runtime error owing to division by zero in if condition
d) Unpredictable behavior of program

Answer: b
Clarification: Operator short circuit and, &&, skips evaluating right hand operand if left hand operand is false thus division by zero in if condition does not give an error.
output:

$ javac Output.java
$ java Output
2

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

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {    
  5.              boolean a = true;
  6.              boolean b = false;
  7.              boolean c = a ^ b;
  8.              System.out.println(!c);
  9.         } 
  10.     }

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

Answer: c
Clarification: None.
output:

$ javac Output.java
$ java Output
false

250+ TOP MCQs on Methods Taking Parameters and Answers

This set of Java Questions and Answers for Entrance exams on “Methods Taking Parameters”.

1. Which of these is the method which is executed first before execution of any other thing takes place in a program?
a) main method
b) finalize method
c) static method
d) private method

Answer: c
Clarification: If a static method is present in the program then it will be executed first, then main will be executed.

2. What is the process of defining more than one method in a class differentiated by parameters?
a) Function overriding
b) Function overloading
c) Function doubling
d) None of the mentioned

Answer: b
Clarification: Function overloading is a process of defining more than one method in a class with same name differentiated by function signature i:e return type or parameters type and number. Example – int volume(int length, int width) & int volume(int length , int width , int height) can be used to calculate volume.

3. Which of these can be used to differentiate two or more methods having the same name?
a) Parameters data type
b) Number of parameters
c) Return type of method
d) All of the mentioned

Answer: d
Clarification: None.

4. Which of these data type can be used for a method having a return statement in it?
a) void
b) int
c) float
d) both int and float

Answer: d
Clarification: None.

5. Which of these statement is incorrect?
a) Two or more methods with same name can be differentiated on the basis of their parameters data type
b) Two or more method having same name can be differentiated on basis of number of parameters
c) Any already defined method in java library can be defined again in the program with different data type of parameters
d) If a method is returning a value the calling statement must have a variable to store that value

Answer: d
Clarification: Even if a method is returning a value, it is not necessary to store that value.

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

  1.     class box 
  2.     {
  3.         int width;
  4.         int height;
  5.         int length;
  6.         int volume;
  7.         void volume(int height, int length, int width) 
  8.         {
  9.              volume = width * height * length;
  10.         } 
  11.     }    
  12.     class Prameterized_method{
  13.         public static void main(String args[]) 
  14.         {
  15.             box obj = new box();
  16.             obj.height = 1;
  17.             obj.length = 5;
  18.             obj.width = 5;
  19.             obj.volume(3, 2, 1);
  20.             System.out.println(obj.volume);        
  21.         } 
  22.     }

a) 0
b) 1
c) 6
d) 25

Answer: c
Clarification: None
output:

$ Prameterized_method.java
$ Prameterized_method
6

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

  1.     class equality 
  2.     {
  3.         int x;
  4.         int y;
  5.         boolean isequal()
  6.         {
  7.             return(x == y);  
  8.         } 
  9.     }    
  10.     class Output 
  11.     {
  12.         public static void main(String args[]) 
  13.         {
  14.             equality obj = new equality();
  15.             obj.x = 5;
  16.             obj.y = 5;
  17.             System.out.println(obj.isequal);
  18.         } 
  19.     }

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

Answer: b
Clarification: None
output:

$ javac Output.java
$ java Output 
true

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

  1.     class box 
  2.     {
  3.         int width;
  4.         int height;
  5.         int length;
  6.         int volume;
  7.         void volume() 
  8.         {
  9.             volume = width * height * length;
  10.         } 
  11.         void volume(int x) 
  12.         {
  13.             volume = x;
  14.         }
  15.     }    
  16.     class Output 
  17.     { 
  18.         public static void main(String args[]) 
  19.         {
  20.             box obj = new box();
  21.             obj.height = 1;
  22.             obj.length = 5;
  23.             obj.width = 5;
  24.             obj.volume(5);
  25.             System.out.println(obj.volume);        
  26.         } 
  27.     }

a) 0
b) 5
c) 25
d) 26

Answer: b
Clarification: None.
output:

$ javac Output.java
$ java Output
5

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

  1.     class Output 
  2.     {
  3.         static void main(String args[]) 
  4.         {    
  5.              int x , y = 1;
  6.              x = 10;
  7.              if(x != 10 && x / 0 == 0)
  8.                  System.out.println(y);
  9.              else
  10.                  System.out.println(++y);
  11.         } 
  12.     }

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

Answer: d
Clarification: main() method must be made public. Without main() being public java run time system will not be able to access main() and will not be able to execute the code.
output:

$ javac Output.java
Error: Main method not found in class Output, please define the main method as:
   public static void main(String[] args)

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

  1.     class area 
  2.     {
  3.         int width;
  4.         int length;
  5.         int height;
  6.         area() 
  7.         {
  8.         width = 5;
  9.         length = 6;
  10.         height = 1;
  11.         }
  12.         void volume() 
  13.         {
  14.              volume = width * height * length;
  15.         } 
  16.     }    
  17.     class cons_method 
  18.     {
  19.         public static void main(String args[]) 
  20.         {
  21.             area obj = new area();
  22.             obj.volume();
  23.             System.out.println(obj.volume);
  24.         } 
  25.     }

a) 0
b) 1
c) 25
d) 30

Answer: d
Clarification: None.
output:

$ javac cons_method.java
$ java cons_method
30

250+ TOP MCQs on Java.lang – Void, Process & System Class and Answers

Java MCQs on Void, Process & System classes of Java Programming Language.

1. Which of these class have only one field ‘TYPE’?
a) Void
b) Process
c) System
d) Runtime

Answer: a
Clarification: The Void class has one field, TYPE, which holds a reference to the Class object for the type void.

2. Which of the following method of Process class can terminate a process?
a) void kill()
b) void destroy()
c) void terminate()
d) void exit()

Answer: b
Clarification: Kills the subprocess. The subprocess represented by this Process object is forcibly terminated.

3. Standard output variable ‘out’ is defined in which class?
a) Void
b) Process
c) Runtime
d) System

Answer: d
Clarification: Standard output variable ‘out’ is defined in System class. out is usually used in print statement i:e System.out.print().

4. Which of these class can encapsulate an entire executing program?
a) Void
b) Process
c) Runtime
d) System

Answer: b
Clarification: None.

5. Which of the following is method of System class is used to find how long a program takes to execute?
a) currenttime()
b) currentTime()
c) currentTimeMillis()
d) currenttimeMillis()

Answer: c
Clarification: None.

6. Which of these class holds a collection of static methods and variables?
a) Void
b) Process
c) Runtime
d) System

Answer: d
Clarification: System class holds a collection of static methods and variables. The standard input, output and error output of java runtime is stored in the in, out and err variables of System class.

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

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             long start, end;   
  6.             start = System.currentTimeMillis();
  7.             for (int i = 0; i < 10000000; i++);
  8.             end = System.currentTimeMillis();
  9.             System.out.print(end - start);
  10.         }
  11.     }

a) 0
b) 1
c) 1000
d) System Dependent

Answer: d
Clarification: end time is the time taken by loop to execute it can be any non zero value depending on the System.
Output:

$ javac Output.java
$ java Output
78

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

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             byte a[] = { 65, 66, 67, 68, 69, 70 };
  6.             byte b[] = { 71, 72, 73, 74, 75, 76 };  
  7.             System.arraycopy(a , 0, b, 0, a.length);
  8.             System.out.print(new String(a) + " " + new String(b));
  9.         }
  10.     }

a) ABCDEF ABCDEF
b) ABCDEF GHIJKL
c) GHIJKL ABCDEF
d) GHIJKL GHIJKL

Answer: a
Clarification: System.arraycopy() is a method of class System which is used to copy a string into another string.
Output:

$ javac Output.java
$ java Output
ABCDEF ABCDEF

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

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             byte a[] = { 65, 66, 67, 68, 69, 70 };
  6.             byte b[] = { 71, 72, 73, 74, 75, 76 };  
  7.             System.arraycopy(a, 2, b, 1, a.length-2);
  8.             System.out.print(new String(a) + " " + new String(b));
  9.         }
  10.     }

a) ABCDEF GHIJKL
b) ABCDEF GCDEFL
c) GHIJKL ABCDEF
d) GCDEFL GHIJKL

Answer: b
Clarification: None.
Output:

$ javac Output.java
$ java Output
ABCDEF GCDEFL

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

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             byte a[] = { 65, 66, 67, 68, 69, 70 };
  6.             byte b[] = { 71, 72, 73, 74, 75, 76 };  
  7.             System.arraycopy(a, 1, b, 3, 0);
  8.             System.out.print(new String(a) + " " + new String(b));
  9.         }
  10.     }

a) ABCDEF GHIJKL
b) ABCDEF GCDEFL
c) GHIJKL ABCDEF
d) GCDEFL GHIJKL

Answer: a
Clarification: Since last parameter of System.arraycopy(a,1,b,3,0) is 0 nothing is copied from array a to array b, hence b remains as it is.
Output:

$ javac Output.java
$ java Output
ABCDEF GHIJKL