Java MCQ Number 01067

Java MCQ Set 1

1. Which of these keyword can be used in subclass to call the constructor of superclass?
a) super
b) this
c) extent
d) extends

Answer

Answer: a [Reason:] None.

2. What is the process of defining a method in subclass having same name & type signature as a method in its superclass?
a) Method overloading
b) Method overriding
c) Method hiding
d) None of the mentioned

Answer

Answer: b [Reason:] None.

3. Which of these keywords can be used to prevent Method overriding?
a) static
b) constant
c) protected
d) final

Answer

Answer: d [Reason:] To disallow a method from being overridden, specify final as a modifier at the start of its declaration. Methods declared as final cannot be overridden.

4. Which of these is correct way of calling a constructor having no parameters, of superclass A by subclass B?
a) super(void);
b) superclass.();
c) super.A();
d) super();

Answer

Answer: d [Reason:] None.

5. At line number 2 below, choose 3 valid data-type attributes/qualifiers among “final, static, native, public, private, abstract, protected”

  1. public interface Status
  2.    {
  3.         /* insert qualifier here */ int MY_VALUE = 10;
  4.    }

a) final, native, private
b) final, static, protected
c) final, private, abstract
d) final, static, public

Answer

Answer: d [Reason:] Every interface variable is implicitly public static and final.

6. Which of these is supported by method overriding in Java?
a) Abstraction
b) Encapsulation
c) Polymorphism
d) None of the mentioned

Answer

Answer: c [Reason:] None.

7. What is the output of this program?

  1.  class Alligator 
  2.  {
  3.   public static void main(String[] args) 
  4.    {
  5.    int []x[] = {{1,2}, {3,4,5}, {6,7,8,9}};
  6.    int [][]y = x;
  7.    System.out.println(y[2][1]);
  8.    }
  9.  }

a) 2
b) 3
c) 7
d) Compilation Error

Answer

Answer: c [Reason:] Both x,and y are pointing to the same array.

8. What is the output of this program?

  1.    final class A 
  2.     {
  3.          int i;
  4.     }    
  5.     class B extends A 
  6.     {
  7.         int j;
  8.         System.out.println(j + " " + i);  
  9.     }    
  10.     class inheritance 
  11.     {
  12.         public static void main(String args[])
  13.         {
  14.             B obj = new B();
  15.             obj.display();     
  16.         }
  17.    }

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

Answer

Answer: d [Reason:] class A has been declared final hence it cannot be inherited by any other class. Hence class B does not have member i, giving compilation error.
output:
$ javac inheritance.java
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
i cannot be resolved or is not a field

9. What is the output of this program?

  1.   class Abc
  2.   {
  3.       public static void main(String[]args)
  4.       {
  5.           String[] elements = { "for", "tea", "too" };
  6.           String first = (elements.length > 0) ? elements[0]: null;
  7.       }
  8.   }

a) Compilation error
b) An exception is thrown at run time
c) The variable first is set to null
d) The variable first is set to elements[0].

Answer

Answer: d [Reason:] The value at the 0th position will be assigned to the variable first.

10. What is the output of this program?

  1.     class A 
  2.     {
  3.         int i;
  4.         public void display() 
  5.         {
  6.             System.out.println(i);
  7.         }    
  8.     }    
  9.     class B extends A 
  10.    {
  11.         int j;
  12.         public void display() 
  13.         {
  14.             System.out.println(j);
  15.         } 
  16.     }    
  17.     class Dynamic_dispatch 
  18.    {
  19.         public static void main(String args[])
  20.         {
  21.             B obj2 = new B();
  22.             obj2.i = 1;
  23.             obj2.j = 2;
  24.             A r;
  25.             r = obj2;
  26.             r.display();     
  27.         }
  28.    }

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

Answer

Answer: b [Reason:] r is reference of type A, the program assigns a reference of object obj2 to r and uses that reference to call function display() of class B.
output:
$ javac Dynamic_dispatch.java
$ java Dynamic_dispatch
2

Java MCQ Set 2

1. Which of these package contains classes and interfaces for networking?
a) java.io
b) java.util
c) java.net
d) java.network

Answer

Answer: c [Reason:] None.

2. Which of these is a protocol for breaking and sending packets to an address across a network?
a) TCIP/IP
b) DNS
c) Socket
d) Proxy Server

Answer

Answer: a [Reason:] TCP/IP – Transfer control protocol/Internet Protocol is used to break data into small packets an send them to an address across a network.

3. How many ports of TCP/IP are reserved for specific protocols?
a) 10
b) 1024
c) 2048
d) 512

Answer

Answer: b [Reason:] None.

4. How many bits are in a single IP address?
a) 8
b) 16
c) 32
d) 64

Answer

Answer: c [Reason:] None.

5. Which of these is a full form of DNS?
a) Data Network Service
b) Data Name Service
c) Domain Network Service
d) Domain Name Service

Answer

Answer: d [Reason:] None.

6. Which of these class is used to encapsulate IP address and DNS?
a) DatagramPacket
b) URL
c) InetAddress
d) ContentHandler

Answer

Answer: c [Reason:] InetAddress class encapsulate both IP address and DNS, we can interact with this class by using name of an IP host.

Java MCQ Set 3

1. Which of these class is superclass of every class in Java?
a) String class
b) Object class
c) Abstract class
d) ArrayList class

Answer

Answer: b [Reason:] Object class is superclass of every class in Java.

2. Which of these method of Object class can clone an object?
a) Objectcopy()
b) copy()
c) Object clone()
d) clone()

Answer

Answer: c [Reason:] None.

3. Which of these method of Object class is used to obtain class of an object at run time?
a) get()
b) void getclass()
c) Class getclass()
d) None of the mentioned

Answer

Answer: c [Reason:] None.

4. Which of these keywords can be used to prevent inheritance of a class?
a) super
b) constant
c) class
d) final

Answer

Answer: d [Reason:] Declaring a class final implicitly declares all of its methods final, and makes the class inheritable.

5. Which of these keywords cannot be used for a class which has been declared final?
a) abstract
b) extends
c) abstract and extends
d) none of the mentioned

Answer

Answer: a [Reason:] A abstract class is incomplete by itself and relies upon its subclasses to provide complete implementation. If we declare a class final then no class can inherit that class, an abstract class needs its subclasses hence both final and abstract cannot be used for a same class.

6. Which of these class relies upon its subclasses for complete implementation of its methods?
a) Object class
b) abstract class
c) ArrayList class
d) None of the mentioned

Answer

Answer: b [Reason:] None.

7. What is the output of this program?

  1.     abstract class A 
  2.     {
  3.         int i;
  4.         abstract void display();
  5.     }    
  6.     class B extends A 
  7.     {
  8.         int j;
  9.         void display() 
  10.         {
  11.             System.out.println(j);
  12.         }
  13.     }    
  14.     class Abstract_demo 
  15.     {
  16.         public static void main(String args[])
  17.         {
  18.             B obj = new B();
  19.             obj.j=2;
  20.             obj.display();    
  21.         }
  22.     }

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

Answer

Answer: b [Reason:] class A is an abstract class, it contains a abstract function display(), the full implementation of display() method is given in its subclass B, Both the display functions are the same. Prototype of display() is defined in class A and its implementation is given in class B.
output:
$ javac Abstract_demo.java
$ java Abstract_demo
2

8. What is the output of this program?

  1.    class A 
  2.    {
  3. int i;
  4. int j;
  5.         A() 
  6.         {
  7.             i = 1;
  8.             j = 2;
  9.         }
  10.    }
  11.    class Output 
  12.    {
  13.         public static void main(String args[])
  14.         {
  15.              A obj1 = new A();
  16.              A obj2 = new A();
  17. 	     System.out.print(obj1.equals(obj2));
  18.         }
  19.    }

a) false
b) true
c) 1
d) Compilation Error

Answer

Answer: a [Reason:] obj1 and obj2 are two different objects. equals() is a method of Object class, Since Object class is superclass of every class it is available to every object.
output:
$ javac Output.java
$ java Output
false

9. What is the output of this program?

  1.     class Output 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.              Object obj = new Object();
  6. 	     System.out.print(obj.getclass());
  7.         }
  8.     }

a) Object
b) class Object
c) class java.lang.Object
d) Compilation Error

Answer

Answer: c [Reason:] None.
output:
$ javac Output.java
$ java Output
class java.lang.Object

10. What is the output of this program?

  1.    class A 
  2.    {
  3.         int i;
  4. int j;
  5.         A() 
  6.         {
  7.             i = 1;
  8.             j = 2;
  9.         }
  10.    }
  11.    class Output 
  12.    {
  13.         public static void main(String args[])
  14.         {
  15.              A obj1 = new A();
  16. 	     System.out.print(obj1.toString());
  17.         }
  18.    }

a) true
b) false
c) String associated with obj1
d) Compilation Error

Answer

Answer: c [Reason:] toString() is method of class Object, since it is superclass of every class, every object has this method. toString() returns the string associated with the calling object.
output:
$ javac Output.java
$ java Output

Java MCQ Set 4

1. What is process of defining two or more methods within same class that have same name but different parameters declaration?
a) method overloading
b) method overriding
c) method hiding
d) none of the mentioned

Answer

Answer: a [Reason:] Two or more methods can have same name as long as their parameters declaration is different, the methods are said to be overloaded and process is called method overloading. Method overloading is a way by which Java implements polymorphism.

2. Which of these can be overloaded?
a) Methods
b) Constructors
c) All of the mentioned
d) None of the mentioned

Answer

Answer: c [Reason:] None.

3. Which of these is correct about passing an argument by call-by-value process?
a) Copy of argument is made into the formal parameter of the subroutine
b) Reference to original argument is passed to formal parameter of the subroutine
c) Copy of argument is made into the formal parameter of the subroutine and changes made on parameters of subroutine have effect on original argument
d) Reference to original argument is passed to formal parameter of the subroutine and changes made on parameters of subroutine have effect on original argument

Answer

Answer: a [Reason:] When we pass an argument by call-by-value a copy of argument is made into the formal parameter of the subroutine and changes made on parameters of subroutine have no effect on original argument, they remain the same.

4. What is the process of defining a method in terms of itself, that is a method that calls itself?
a) Polymorphism
b) Abstraction
c) Encapsulation
d) Recursion

Answer

Answer: d [Reason:] None.

5. What is the output of the following code?

  1. class San
  2. {
  3.  public void m1 (int i,float f)
  4.  {
  5.   System.out.println(" int float method");
  6.  }
  7.  
  8.  public void m1(float f,int i);
  9.   {
  10.   System.out.println("float int method");
  11.   }
  12.  
  13.   public static void main(String[]args)
  14.   {
  15.     San s=new San();
  16.         s.m1(20,20);
  17.   }
  18. }

a) int float method
b) float int method
c) compile time error
d) run time error

Answer

Answer: c [Reason:] While resolving overloaded method, compiler automatically promotes if exact match is not found. But in this case, which one to promote is an ambiguity.

6. What is the output of this program?

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

a) 5
b) 6
c) 7
d) 8

Answer

Answer: c [Reason:] None.
output:
$ javac Overload_methods.java
$ java Overload_methods
7

7. What is the output of this program?

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

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

Answer

Answer: c [Reason:] None.
output:
$ javac Overload_methods.java
$ java Overload_methods
8

8. What is the output of this program?

  1.    class overload 
  2.    {
  3.         int x;
  4.  double y;
  5.         void add(int a , int b) 
  6.         {
  7.             x = a + b;
  8.         }
  9.         void add(double c , double d)
  10.         {
  11.             y = c + d;
  12.         }
  13.         overload() 
  14.         {
  15.             this.x = 0;
  16.             this.y = 0;
  17.         }        
  18.     }    
  19.     class Overload_methods 
  20.     {
  21.         public static void main(String args[])
  22.         {
  23.             overload obj = new overload();   
  24.             int a = 2;
  25.             double b = 3.2;
  26.             obj.add(a, a);
  27.             obj.add(b, b);
  28.             System.out.println(obj.x + " " + obj.y);     
  29.         }
  30.    }

a) 6 6
b) 6.4 6.4
c) 6.4 6
d) 4 6.4

Answer

Answer: d [Reason:] For obj.add(a,a); ,the function in line number 4 gets executed and value of x is 4. For the next function call, the function in line number 7 gets executed and value of y is 6.4
output:
$ javac Overload_methods.java
$ java Overload_methods
4 6.4

9. What is the output of this program?

  1.     class test 
  2.     {
  3.         int a;
  4.         int b;
  5.         void meth(int i , int j) 
  6.         {
  7.             i *= 2;
  8.             j /= 2;
  9.         }          
  10.     }    
  11.     class Output 
  12.     {
  13.         public static void main(String args[])
  14.         {
  15.             test obj = new test();
  16. 	    int a = 10;
  17.             int b = 20;             
  18.             obj.meth(a , b);
  19.             System.out.println(a + " " + b);        
  20.         } 
  21.     }

a) 10 20
b) 20 10
c) 20 40
d) 40 20

Answer

Answer: a [Reason:] Variables a & b are passed by value, copy of their values are made on formal parameters of function meth() that is i & j. Therefore changes done on i & j are not reflected back on original arguments. a & b remain 10 & 20 respectively.
output:
$ javac Output.java
$ java Output
10 20

10. What is the output of this program?

  1.     class test 
  2.     {
  3.         int a;
  4.         int b;
  5.         test(int i, int j) 
  6.         {
  7.             a = i;
  8.             b = j;
  9.         }
  10.         void meth(test o) 
  11.         {
  12.             o.a *= 2;
  13.             O.b /= 2;
  14.         }          
  15.     }    
  16.     class Output 
  17.     {
  18.         public static void main(String args[])
  19.         {
  20.             test obj = new test(10 , 20);
  21.             obj.meth(obj);
  22.             System.out.println(obj.a + " " + obj.b);        
  23.         } 
  24.     }

a) 10 20
b) 20 10
c) 20 40
d) 40 20

Answer

Answer: b [Reason:] Class objects are always passed by reference, therefore changes done are reflected back on original arguments. obj.meth(obj) sends object obj as parameter whose variables a & b are multiplied and divided by 2 respectively by meth() function of class test. a & b becomes 20 & 10 respectively.
output:
$ javac Output.java
$ java Output
20 10

Java MCQ Set 5

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

Answer: b [Reason:] Recursion is the process of defining something in terms of itself. It allows us to define 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

Answer: b [Reason:] 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

Answer: a [Reason:] If a recursive method does not have a base case then an infinite loop occurs which results in stackoverflow.

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’s Run – Time environment

Answer

Answer: d [Reason:] Recursion is always managed by operating system.

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

Answer

Answer: a [Reason:] None.

6. What is the output of this 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

Answer: d [Reason:] Since the base case of the recursive function func() is not defined hence infinite loop occurs and results in stackoverflow.
Output:
$ javac Output.javac
$ java Output
Exception in thread “main” java.lang.StackOverflowError

7. What is the output of this 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

Answer: b [Reason:] None.
Output:
$ javac Output.javac
$ java Output
1

8. What is the output of this 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

Answer: c [Reason:] 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 is the output of this 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

Answer: a [Reason:] 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 is the output of this 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

Answer: d [Reason:] None.
Output:
$ javac Output.javac
$ java Output
720

ed010d383e1f191bdb025d5985cc03fc?s=120&d=mm&r=g

DistPub Team

Distance Publisher (DistPub.com) provide project writing help from year 2007 and provide writing and editing help to hundreds student every year.