Vidyalelo
Java Programming · all questions

Overriding and Overloading
practice.

Practice every MCQ with options. Use Show answers when you want the correct option and solution.

62

Questions

3/4

Page

Pick an option on a question to see the right answer and solution.

What is the output for the below code?
class A{
      private void printName(){
            System.out.println("Value-A");
      }
}
class B extends A{
      public void printName(){
            System.out.println("Name-B");
      }
}
public class Test{
      public static void main (String[] args){
            B b = new B();
            b.printName();
      }
}

Select an option to see the answer and solution.

What is the output for the below code?
class A{
      public A(){
            System.out.println("A");
      }
      public A(int i){
            this();
            System.out.println(i);
      }
}
class B extends A{
      public B(){
            System.out.println("B");
      }
      public B(int i){
            this();
            System.out.println(i+3);
      }
}
public class Test{
      public static void main (String[] args){
            new B(5);
      }
}

Select an option to see the answer and solution.

Which of these keywords can be used to prevent Method overriding?

Select an option to see the answer and solution.

What will be the output of the following Java program?
final class A 
 {
      int i;
 }    
 class B extends A 
 {
     int j;
     System.out.println(j + " " + i);  
 }    
 class inheritance 
 {
     public static void main(String args[])
     {
         B obj = new B();
         obj.display();     
     }
}

Select an option to see the answer and solution.

What will be the output of the following Java program?
class Alligator 
{
 public static void main(String[] args) 
  {
  int []x[] = {{1,2}, {3,4,5}, {6,7,8,9}};
  int [][]y = x;
  System.out.println(y[2][1]);
  }
}

Select an option to see the answer and solution.

What will be the output of the following Java code?
class overload 
{
    int x;
int y;
    void add(int a)
    {
        x =  a + 1;
    }
    void add(int a , int b)
    {
        x =  a + 2;
    }        
}    
class Overload_methods 
{
    public static void main(String args[])
    {
        overload obj = new overload();   
        int a = 0;
        obj.add(6, 7);
        System.out.println(obj.x);     
    }
}

Select an option to see the answer and solution.

What is the process of defining a method in a subclass having same name & type signature as a method in its superclass?

Select an option to see the answer and solution.

What will be the output of the following Java code?
class A
{
 public void m1 (int i,float f)
 {
  System.out.println(" int float method");
 }
 
 public void m1(float f,int i);
  {
  System.out.println("float int method");
  }
 
  public static void main(String[]args)
  {
    A a=new A();
        a.m1(20,20);
  }
}

Select an option to see the answer and solution.

Which of these is correct way of calling a constructor having no parameters, of superclass A by subclass B?

Select an option to see the answer and solution.

What is the process of defining two or more methods within same class that have same name but different parameters declaration?

Select an option to see the answer and solution.

What will be the output of the following Java code?
class test 
{
    int a;
    int b;
    test(int i, int j) 
    {
        a = i;
        b = j;
    }
    void meth(test o) 
    {
        o.a *= 2;
        O.b /= 2;
    }          
}    
class Output 
{
    public static void main(String args[])
    {
        test obj = new test(10 , 20);
        obj.meth(obj);
        System.out.println(obj.a + " " + obj.b);        
    } 
}

Select an option to see the answer and solution.

Which of these is correct about passing an argument by call-by-value process?

Select an option to see the answer and solution.

What will be the output of the following Java code?
class overload 
{
    int x;
int y;
    void add(int a) 
    {
        x =  a + 1;
    }
    void add(int a, int b)
    {
        x =  a + 2;
    }        
}    
class Overload_methods 
{
    public static void main(String args[])
    {
        overload obj = new overload();   
        int a = 0;
        obj.add(6);
        System.out.println(obj.x);     
    }
}

Select an option to see the answer and solution.

What will be the output of the following Java code?
class overload 
{
     int x;
double y;
     void add(int a , int b) 
     {
         x = a + b;
     }
     void add(double c , double d)
     {
         y = c + d;
     }
     overload() 
     {
         this.x = 0;
         this.y = 0;
     }        
 }    
 class Overload_methods 
 {
     public static void main(String args[])
     {
         overload obj = new overload();   
         int a = 2;
         double b = 3.2;
         obj.add(a, a);
         obj.add(b, b);
         System.out.println(obj.x + " " + obj.y);     
     }
}

Select an option to see the answer and solution.

Which of these is supported by method overriding in Java?

Select an option to see the answer and solution.

What is the process of defining a method in terms of itself, that is a method that calls itself?

Select an option to see the answer and solution.

Which of this keyword can be used in a subclass to call the constructor of superclass?

Select an option to see the answer and solution.

What will be the output of the following Java program?
class Abc
{
    public static void main(String[]args)
    {
        String[] elements = { "for", "tea", "too" };
        String first = (elements.length > 0) ? elements[0]: null;
    }
}

Select an option to see the answer and solution.

Which of these can be overloaded?

Select an option to see the answer and solution.

At line number 2 in the following code, choose 3 valid data-type attributes/qualifiers among "final, static, native, public, private, abstract, protected"
public interface Status
   {
        /* insert qualifier here */ int MY_VALUE = 10;
   }

Select an option to see the answer and solution.