250+ TOP MCQs on Arrays Revisited & Keyword static and Answers

This set of Java Question Bank on “Arrays Revisited & Keyword static”.

1. Arrays in Java are implemented as?
a) class
b) object
c) variable
d) none of the mentioned

Answer: b
Clarification: None.

2. Which of these keywords is used to prevent content of a variable from being modified?
a) final
b) last
c) constant
d) static

Answer: a
Clarification: A variable can be declared final, doing so prevents its content from being modified. Final variables must be initialized when it is declared.

3. Which of these cannot be declared static?
a) class
b) object
c) variable
d) method

Answer: b
Clarification: static statements are run as soon as class containing then is loaded, prior to any object declaration.

4. Which of the following statements are incorrect?
a) static methods can call other static methods only
b) static methods must only access static data
c) static methods can not refer to this or super in any way
d) when object of class is declared, each object contains its own copy of static variables

Answer: d
Clarification: All objects of class share same static variable, when object of a class are declared, all the objects share same copy of static members, no copy of static variables are made.

5. Which of the following statements are incorrect?
a) Variables declared as final occupy memory
b) final variable must be initialized at the time of declaration
c) Arrays in java are implemented as an object
d) All arrays contain an attribute-length which contains the number of elements stored in the array

Answer: a
Clarification: None.

6. Which of these methods must be made static?
a) main()
b) delete()
c) run()
d) finalize()

Answer: a
Clarification: main() method must be declared static, main() method is called by Java runtime system before any object of any class exists.

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

  1.     class access
  2.     {
  3.         public int x;
  4.  	static int y;
  5.         void cal(int a, int b)
  6.         {
  7.             x +=  a ;
  8.             y +=  b;
  9.         }        
  10.     }    
  11.     class static_specifier 
  12.     {
  13.         public static void main(String args[])
  14.         {
  15.             access obj1 = new access();
  16.             access obj2 = new access();   
  17.             obj1.x = 0;
  18.             obj1.y = 0;
  19.             obj1.cal(1, 2);
  20.             obj2.x = 0;
  21.             obj2.cal(2, 3);
  22.             System.out.println(obj1.x + " " + obj2.y);     
  23.         }
  24.    }

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

Answer: d
Clarification: None.
output:

$ javac static_specifier.java
$ java static_specifier
1 5

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

  1.     class access
  2.     {
  3.        static int x;
  4.        void increment()
  5.        {
  6.            x++;
  7.        }   
  8.      }   
  9.     class static_use 
  10.     {
  11.         public static void main(String args[])
  12.         {
  13.             access obj1 = new access();
  14.             access obj2 = new access();
  15.             obj1.x = 0;   
  16.             obj1.increment();
  17.             obj2.increment();
  18.             System.out.println(obj1.x + " " + obj2.x);
  19.          }
  20.    }

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

Answer: c
Clarification: All objects of class share same static variable, all the objects share same copy of static members, obj1.x and obj2.x refer to same element of class which has been incremented twice and its value is 2.
output:

$ javac static_use.java
$ java static_use 
2 2

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

  1.     class static_out 
  2.     {
  3.         static int x;
  4.  	static int y;
  5.         void add(int a , int b)
  6.         {
  7.             x = a + b;
  8.             y = x + b;
  9.         }
  10.     }    
  11.     class static_use 
  12.     {
  13.         public static void main(String args[])
  14.         {
  15.             static_out obj1 = new static_out();
  16.             static_out obj2 = new static_out();   
  17.             int a = 2;
  18.             obj1.add(a, a + 1);
  19.             obj2.add(5, a);
  20.             System.out.println(obj1.x + " " + obj2.y);     
  21.         }
  22.    }

a) 7 7
b) 6 6
c) 7 9
d) 9 7

Answer: c
Clarification: None.
output:

$ javac static_use.java
$ java static_use
7 9

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

  1.     class Output 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             int arr[] = {1, 2, 3, 4, 5};
  6.             for ( int i = 0; i < arr.length - 2; ++i)
  7.                 System.out.println(arr[i] + " ");
  8.         } 
  9.     }

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

Answer: b
Clarification: arr.length() is 5, so the loop is executed for three times.
output:

$ javac Output.java
$ java Output
1 2 3

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

  1.     class Output 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             int a1[] = new int[10];
  6.             int a2[] = {1, 2, 3, 4, 5};
  7.             System.out.println(a1.length + " " + a2.length);
  8.         } 
  9.     }

a) 10 5
b) 5 10
c) 0 10
d) 0 5

Answer: a
Clarification: Arrays in java are implemented as objects, they contain an attribute that is length which contains the number of elements that can be stored in the array. Hence a1.length gives 10 and a2.length gives 5.
output:

$ javac Output.java
$ java Output
10 5

250+ TOP MCQs on Java.lang Introduction and Answers

Java MCQs on java.lang library of Java Programming Language.

1. Which of these classes is not included in java.lang?
a) Byte
b) Integer
c) Array
d) Class

Answer: c
Clarification: Array class is a member of java.util.

2. Which of these is a process of converting a simple data type into a class?
a) type wrapping
b) type conversion
c) type casting
d) none of the Mentioned

Answer: a
Clarification: None.

3. Which of these is a super class of wrappers Double & Integer?
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.

4. Which of these is a wrapper for simple data type float?
a) float
b) double
c) Float
d) Double

Answer: c
Clarification: None.

5. Which of the following is a method of wrapper Float for converting the value of an object into byte?
a) bytevalue()
b) byte byteValue()
c) Bytevalue()
d) Byte Bytevalue()

Answer: b
Clarification: None.

6. Which of these methods is used to check for infinitely large and small values?
a) isInfinite()
b) isNaN()
c) Isinfinite()
d) IsNaN()

Answer: a
Clarification: isinfinite() method returns true is the value being tested is infinitely large or small in magnitude.

7. Which of the following package stores all the simple data types in java?
a) lang
b) java
c) util
d) java.packages

Answer: a
Clarification: None.

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

  1.     class isinfinite_output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             Double d = new Double(1 / 0.);  
  6.             boolean x = d.isInfinite();
  7.             System.out.print(x);
  8.         }
  9.     }

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

Answer: c
Clarification: isInfinite() method returns true is the value being tested is infinitely large or small in magnitude. 1/0. is infinitely large in magnitude hence true is stored in x.
Output:

$ javac isinfinite_output.java
$ java isinfinite_output
true

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

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

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

Answer: d
Clarification: isisNaN() method returns true is the value being tested is a number. 1/0. is infinitely large in magnitude, which cannot be defined as a number hence false is stored in x.
Output:

$ javac isNaN_output.java
$ java isNaN_output
false

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

  1.     class binary 
  2.     {
  3.          public static void main(String args[]) 
  4.          {
  5.              int num = 17;
  6.              System.out.print(Integer.toBinaryString(num));
  7.          }
  8.     }

a) 1001
b) 10011
c) 11011
d) 10001

Answer: d
Clarification: None.
output:

$ javac binary.java
$ java binary 
10001

250+ TOP MCQs on Java.lang – Class and Answers

Java MCQs Class of java.lang library of Java Programming Language.

1. Which of these classes encapsulate runtime state of an object?
a) Class
b) System
c) Runtime
d) Cache

Answer: a
Clarification: None.

2. Which of these methods returns the class of an object?
a) getClass()
b) Class()
c) WhoseClass()
d) WhoseObject()

Answer: a
Clarification: None.

3. Which of these methods return a class object given its name?
a) getClass()
b) findClass()
c) getSystemClass()
d) findSystemClass()

Answer: d
Clarification: findSystemClass() returns a class object given its name.

4. Which of these class defines how the classes are loaded?
a) Class
b) System
c) Runtime
d) ClassLoader

Answer: d
Clarification: None.

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

  1.     class X 
  2.     {
  3.         int a;
  4.         double b;
  5.     }
  6.     class Y extends X 
  7.     {
  8. 	int c;
  9.     }
  10.     class Output 
  11.     {
  12.         public static void main(String args[]) 
  13.         {
  14.             X a = new X();
  15.             Y b = new Y();
  16.             Class obj;
  17.             obj = a.getClass();
  18.             System.out.print(obj.getName());
  19.         }
  20.     }

a) X
b) Y
c) a
d) b

Answer: a
Clarification: getClass() is used to obtain the class of an object, here ‘a’ is an object of class ‘X’. hence a.getClass() returns ‘X’ which is stored in class Class object obj.
Output:

$ javac Output.java
$ java Output
X

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

  1.     class X
  2.     {
  3.         int a;
  4.         double b;
  5.     }
  6.     class Y extends X 
  7.     {
  8. 	int c;
  9.     }
  10.     class Output 
  11.     {
  12.         public static void main(String args[]) 
  13.         {
  14.             X a = new X();
  15.             Y b = new Y();
  16.             Class obj;
  17.             obj = b.getClass();
  18.             System.out.print(obj.getSuperclass());
  19.         }
  20.     }

a) X
b) Y
c) class X
d) class Y

Answer: c
Clarification: getSuperClass() returns the super class of an object. b is an object of class Y which extends class X, Hence Super class of b is X. therefore class X is printed.
Output:

$ javac Output.java
$ java Output
class X

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

  1.     class X 
  2.     {
  3.         int a;
  4.         double b;
  5.     }
  6.     class Y extends X 
  7.     {
  8. 	int c;
  9.     }
  10.     class Output 
  11.     {
  12.         public static void main(String args[]) 
  13.         {
  14.             X a = new X();
  15.             Y b = new Y();
  16.             Class obj;
  17.             obj = b.getClass();
  18.             System.out.print(obj.isLocalClass());
  19.         }
  20.     }

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

Answer: d
Clarification: None.
Output:

$ javac Output.java
$ java Output
false

250+ TOP MCQs on Java.util – Vectors & Stack and Answers

Java MCQs on Vectors & Stack of Java Programming Language.

1. Which of these class object can be used to form a dynamic array?
a) ArrayList
b) Map
c) Vector
d) ArrayList & Vector

Answer: d
Clarification: Vectors are dynamic arrays, it contains many legacy methods that are not part of collection framework, and hence these methods are not present in ArrayList. But both are used to form dynamic arrays.

2. Which of these are legacy classes?
a) Stack
b) Hashtable
c) Vector
d) All of the mentioned

Answer: d
Clarification: Stack, Hashtable, Vector, Properties and Dictionary are legacy classes.

3. Which of these is the interface of legacy?
a) Map
b) Enumeration
c) HashMap
d) Hashtable

Answer: b
Clarification: None.

4. What is the name of a data member of class Vector which is used to store a number of elements in the vector?
a) length
b) elements
c) elementCount
d) capacity

Answer: c
Clarification: None.

5. Which of these methods is used to add elements in vector at specific location?
a) add()
b) set()
c) AddElement()
d) addElement()

Answer: d
Clarification: addElement() is used to add data in the vector, to obtain the data we use elementAt() and to first and last element we use firstElement() and lastElement() respectively.

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

  1.     import java.util.*;
  2.     class vector 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             Vector obj = new Vector(4,2);
  7.             obj.addElement(new Integer(3));
  8.             obj.addElement(new Integer(2));
  9.             obj.addElement(new Integer(5));
  10.             System.out.println(obj.elementAt(1));
  11.         }
  12.     }

a) 0
b) 3
c) 2
d) 5

Answer: c
Clarification: obj.elementAt(1) returns the value stored at index 1, which is 2.
Output:

$ javac vector.java
$ java vector
2

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

  1.     import java.util.*;
  2.     class vector 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             Vector obj = new Vector(4,2);
  7.             obj.addElement(new Integer(3));
  8.             obj.addElement(new Integer(2));
  9.             obj.addElement(new Integer(5));
  10.             System.out.println(obj.capacity());
  11.         }
  12.     }

a) 2
b) 3
c) 4
d) 6

Answer: c
Clarification: None.
Output:

$ javac vector.java
$ java vector
4

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

  1.     import java.util.*;
  2.     class vector 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             Vector obj = new Vector(4,2);
  7.             obj.addElement(new Integer(3));
  8.             obj.addElement(new Integer(2));
  9.             obj.addElement(new Integer(6));
  10.             obj.insertElementAt(new Integer(8), 2);
  11.             System.out.println(obj);
  12.         }
  13.     }

a) [3, 2, 6]
b) [3, 2, 8]
c) [3, 2, 6, 8]
d) [3, 2, 8, 6]

Answer: d
Clarification: None.
Output:

$ javac vector.java
$ java vector
[3, 2, 8, 6].

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

  1.     import java.util.*;
  2.     class vector 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             Vector obj = new Vector(4,2);
  7.             obj.addElement(new Integer(3));
  8.             obj.addElement(new Integer(2));
  9.             obj.addElement(new Integer(5));
  10.             obj.removeAll(obj);
  11.             System.out.println(obj.isEmpty());
  12.         }
  13.     }

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

Answer: c
Clarification: firstly elements 3, 2, 5 are entered in the vector obj, but when obj.removeAll(obj); is executed all the elements are deleted and vector is empty, hence obj.isEmpty() returns true.
Output:

$ javac vector.java
$ java vector
true

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

  1.     import java.util.*;
  2.     class stack 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             Stack obj = new Stack();
  7.             obj.push(new Integer(3));
  8.             obj.push(new Integer(2));
  9.             obj.pop();
  10.             obj.push(new Integer(5));
  11.      	    System.out.println(obj);
  12.         }
  13.     }

a) [3, 5]
b) [3, 2]
c) [3, 2, 5]
d) [3, 5, 2]

Answer: a
Clarification: push() and pop() are standard functions of the class stack, push() inserts in the stack and pop removes from the stack. 3 & 2 are inserted using push() the pop() is used which removes 2 from the stack then again push is used to insert 5 hence stack contains elements 3 & 5.
Output:

$ javac stack.java
$ java stack
[3, 5].

250+ TOP MCQs on Implementing Runnable interface for Threads and Answers

This set of Java MCQs on “Implementing Runnable interface for Threads”.

1. Which of these method is used to implement Runnable interface?
a) stop()
b) run()
c) runThread()
d) stopThread()

Answer: b
Clarification: To implement Runnable interface, a class needs only to implement a single method called run().

2. Which of these method is used to begin the execution of a thread?
a) run()
b) start()
c) runThread()
d) startThread()

Answer: b
Clarification: None.

3. Which of these statement is incorrect?
a) A thread can be formed by implementing Runnable interface only
b) A thread can be formed by a class that extends Thread class
c) start() method is used to begin execution of the thread
d) run() method is used to begin execution of a thread before start() method in special cases

Answer: d
Clarification: run() method is used to define the code that constitutes the new thread, it contains the code to be executed. start() method is used to begin execution of the thread that is execution of run(). run() itself is never used for starting execution of the thread.

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

  1.     class newthread implements Runnable 
  2.     {
  3. 	Thread t;
  4. 	newthread() 
  5.         {
  6. 	    t = new Thread(this,"My Thread");
  7. 	    t.start();
  8. 	}
  9. 	public void run()
  10.         {
  11. 	    System.out.println(t.getName());
  12. 	}
  13.     }
  14.     class multithreaded_programing
  15.     {
  16.         public static void main(String args[])
  17.         {
  18.             new newthread();        
  19.         }
  20.     }

a) My Thread
b) Thread[My Thread,5,main]
c) Compilation Error
d) Runtime Error

Answer: a
Clarification: None.
Output:

$ javac multithreaded_programing.java
$ java multithreaded_programing
My Thread

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

  1.     class newthread implements Runnable
  2.     {
  3. 	Thread t;
  4. 	newthread()
  5.         {
  6. 	    t = new Thread(this,"My Thread");
  7. 	    t.start();
  8. 	}
  9. 	public void run()
  10.         {
  11. 	    System.out.println(t);
  12. 	}
  13.     }
  14.     class multithreaded_programing
  15.     {
  16.         public static void main(String args[])
  17.         {
  18.             new newthread();        
  19.         }
  20.     }

a) My Thread
b) Thread[My Thread,5,main]
c) Compilation Error
d) Runtime Error

Answer: b
Clarification: None.
Output:

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

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

  1.     class newthread implements Runnable
  2.     {
  3. 	Thread t;
  4. 	newthread()
  5.         {
  6. 	    t = new Thread(this,"My Thread");
  7. 	    t.start();
  8. 	}
  9.     }
  10.     class multithreaded_programing
  11.     {
  12.         public static void main(String args[])
  13.         {
  14.             new newthread();        
  15.         }
  16.     }

a) My Thread
b) Thread[My Thread,5,main]
c) Compilation Error
d) Runtime Error

Answer: c
Clarification: Thread t has been made by using Runnable interface, hence it is necessary to use inherited abstract method run() method to specify instructions to be implemented on the thread, since no run() method is used it gives a compilation error.
Output:

$ javac multithreaded_programing.java
The type newthread must implement the inherited abstract method Runnable.run()

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

  1.     class newthread implements Runnable
  2.     {
  3. 	Thread t;
  4. 	newthread()
  5.         {
  6. 	    t = new Thread(this,"New Thread");
  7. 	    t.start();
  8. 	}
  9. 	public void run()
  10.         {
  11. 	    t.setPriority(Thread.MAX_PRIORITY);	
  12.             System.out.println(t);
  13. 	}
  14.     }
  15.     class multithreaded_programing
  16.     {
  17.         public static void main(String args[])
  18.         {
  19.             new newthread();        
  20.         }
  21.     }

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

Answer: d
Clarification: Thread t has been made with default priority value 5 but in run method the priority has been explicitly changed to MAX_PRIORITY of class thread, that is 10 by code ‘t.setPriority(Thread.MAX_PRIORITY);’ using the setPriority function of thread t.
Output:

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

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

  1.     class newthread implements Runnable
  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: Threads t1 & t2 are created by class newthread that is implementing runnable interface, hence both the threads are provided their own run() method specifying the actions to be taken. When constructor of newthread class is called first the run() method of t1 executes than the run method of t2 printing 2 times “false” as both the threads are not equal one is having different priority than other, hence falsefalse is printed.
Output:

$ javac multithreaded_programing.java
$ java multithreaded_programing
falsefalse

250+ TOP MCQs on Random Number and Answers

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

1. Which class is used to generate random number?
a) java.lang.Object
b) java.util.randomNumber
c) java.util.Random
d) java.util.Object

Answer: c
Clarification: java.util.random class is used to generate random numbers in java program.

2. Which method is used to generate boolean random values in java?
a) nextBoolean()
b) randomBoolean()
c) previousBoolean()
d) generateBoolean()

Answer: a
Clarification: nextBoolean() method of java.util.Random class is used to generate random numbers.

3. What is the return type of Math.random() method?
a) Integer
b) Double
c) String
d) Boolean

Answer: b
Clarification: Math.random() method returns floating point number or precisely a double.

4. Random is a final class?
a) True
b) False

Answer: b
Clarification: Random is not a final class and can be extended to implement the algorithm as per requirement.

5. What is the range of numbers returned by Math.random() method?
a) -1.0 to 1.0
b) -1 to 1
c) 0 to 100
d) 0.0 to 1.0

Answer: d
Clarification: Math.random() returns only double value greater than or equal to 0.0 and less than 1.0.

6. How many bits are used for generating random numbers?
a) 32
b) 64
c) 48
d) 8

Answer: c
Clarification: Random number can accept 64 bits but it only uses 48 bits for generating random numbers.

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

int a = random.nextInt(15) + 1;

a) Random number between 1 to 15, including 1 and 15
b) Random number between 1 to 15, excluding 15
c) Random number between 1 to 15, excluding 1
d) Random number between 1 to 15, excluding 1 and 15

Answer: a
Clarification: random.nextInt(15) + 1; returns random numbers between 1 to 15 including 1 and 15.

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

int a = random.nextInt(7) + 4;

a) Random number between 4 to 7, including 4 and 7
b) Random number between 4 to 7, excluding 4 and 7
c) Random number between 4 to 10, excluding 4 and 10
d) Random number between 4 to 10, including 4 and 10

Answer: d
Clarification: random.nextInd(7) + 4; returns random numbers between 4 to 10 including 4 and 10. it follows “nextInt(max – min +1) + min” formula.

9. Math.random() guarantees uniqueness?
a) True
b) False

Answer: b
Clarification: Math.random() doesn’t guarantee uniqueness. To guarantee uniqueness we must store the generated value in the database and compare against already generated values.

10. What is the signature of Math.random() method?
a) public static double random()
b) public void double random()
c) public static int random()
d) public void int random()

Answer: a
Clarification: public static double random() is the utility method provided by Math class which returns double.

contest