• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

50 Java Language MCQs with Answers

Question 21

Java
final class Complex {
    private  double re,  im;
    public Complex(double re, double im) {
        this.re = re;
        this.im = im;
    }
    Complex(Complex c)
    {
      System.out.println(\"Copy constructor called\");
      re = c.re;
      im = c.im;
    }            
    public String toString() {
        return \"(\" + re + \" + \" + im + \"i)\";
    }            
}
class Main {
    public static void main(String[] args) {
        Complex c1 = new Complex(10, 15);
        Complex c2 = new Complex(c1);    
        Complex c3 = c1;  
        System.out.println(c2);
    }
}
  • Copy constructor called
    (10.0 + 15.0i)
  • Copy constructor called
    (0.0 + 0.0i)
  • (10.0 + 15.0i)
  • (0.0 + 0.0i)

Question 22

Output of following Java program Java
class Point {
  int m_x, m_y;
  
  public Point(int x, int y) { m_x = x; m_y = y; }
  public Point() { this(10, 10); }
  public int getX() { return m_x; }
  public int getY() { return m_y; }
  
  public static void main(String args[]) {
    Point p = new Point();
    System.out.println(p.getX());
  }
} 
  • 10
  • 0
  • compiler error

Question 23

Which of the following is/are true about constructors in Java?

1) Constructor name should be same as class name.
2) If you don\'t define a constructor for a class, 
    a default parameterless constructor is automatically
    created by the compiler. 
3) The default constructor calls super() and initializes all 
   instance variables to default value like 0, null.
4) If we want to parent class constructor, it must be called in 
   first line of constructor.
  • 1
  • 1, 2
  • 1, 2 and 3
  • 1, 2, 3 and 4

Question 24

Predict the output? Java
package main;
class T {
  int t = 20;
}
class Main {
   public static void main(String args[]) {
      T t1 = new T();
      System.out.println(t1.t);
   }
}
  • 20
  • 0
  • Compiler Error

Question 25

The built-in base class in Java, which is used to handle all exceptions is
  • Raise
  • Exception
  • Error
  • Throwable

Question 26

Which of these is a super class of all errors and exceptions in the Java language?
  • RunTimeExceptions
  • Throwable
  • Catchable
  • None of the above

Question 27

Predict the output of the following program. Java
class Test
{   int count = 0;

    void A() throws Exception
    {
        try
        {
            count++;
            
            try
            {
                count++;

                try
                {
                    count++;
                    throw new Exception();

                }
                
                catch(Exception ex)
                {
                    count++;
                    throw new Exception();
                }
            }
            
            catch(Exception ex)
            {
                count++;
            }
        }
        
        catch(Exception ex)
        {
            count++;
        }

    }

    void display()
    {
        System.out.println(count);
    }

    public static void main(String[] args) throws Exception
    {
        Test obj = new Test();
        obj.A();
        obj.display();
    }
}
  • 4
  • 5
  • 6
  • Compilation error

Question 28

Java
class Base extends Exception {}
class Derived extends Base  {}

public class Main {
  public static void main(String args[]) {
   // some other stuff
   try {
       // Some monitored code
       throw new Derived();
    }
    catch(Base b)     { 
       System.out.println(\"Caught base class exception\"); 
    }
    catch(Derived d)  { 
       System.out.println(\"Caught derived class exception\"); 
    }
  }
} 
  • Caught base class exception
  • Caught derived class exception
  • Compiler Error because derived is not throwable
  • Compiler Error because base class exception is caught before derived class

Question 29

Predict the output of following Java program Java
class Main {
   public static void main(String args[]) {
      try {
         throw 10;
      }
      catch(int e) {
         System.out.println(\"Got the  Exception \" + e);
      }
  }
}
  • Got the Exception 10
  • Got the Exception 0
  • Compiler Error

Question 30

Which of the following is/are true about packages in Java?
1) Every class is part of some package. 
2) All classes in a file are part of the same package. 
3) If no package is specified, the classes in the file 
   go into a special unnamed package 
4) If no package is specified, a new package is created with 
   folder name of class and the class is put in this package. 
  • Only 1, 2 and 3
  • Only 1, 2 and 4
  • Only 4
  • Only 1 and 3

There are 50 questions to complete.

Last Updated :
Take a part in the ongoing discussion