Vidyalelo
Java Programming · all questions

Constructors and Methods
practice.

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

140

Questions

3/7

Page

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

Determine output of the following program.
public class Test{
           public static void main(String args[]){
                  System.out.println( Math.floor( Math.random( ) ) ) ;
           }
}

Select an option to see the answer and solution.

What is the output of the above program?
class Num  {
      Num(double x ){
              System.out.println( x ) ;
      }   
}
public class  Test  extends  Num   {
       public static void main(String[] args){
                  Num num = new Num( 2 ) ;
       }     
}

Select an option to see the answer and solution.

Determine Output:
class A{
	public static void method(int i){
		System.out.print("Method 1");
	}

	public static int method(String str){
		System.out.print("Method 2");
		return 0;
	}
}

public class Test{
     
	public static void main(String args[]){
		A.method(5);
	}
}

Select an option to see the answer and solution.

What is the output of the program?
class Test{ 
        public int display(int x, int y){ 
                return ("The sum of x and y is " + x + y); 
        } 

        public static void main(String args[]){ 
                Test test = new Test();
                System.out.println(test.display(4,5)); 
        } 
}

Select an option to see the answer and solution.

The implicit return type of a constructor is

Select an option to see the answer and solution.

The finalize() method is called just prior to

Select an option to see the answer and solution.

The main method should be static for the reason

Select an option to see the answer and solution.

Given the following piece of code:
class Person{
        public int number;
}
public class Test{
        public void doIt(int i , Person p){
                i = 5;
                p.number = 8;
        }
        public static void main(String args[]){
                int x = 0;
                Person p = new Person();
                new Test().doIt(x, p);
                System.out.println(x + " " + p.number);
        }
}

What is the result?

Select an option to see the answer and solution.

Which of the following statements regarding static methods are correct?
1. Static methods are difficult to maintain, because you can not change their implementation.
2. Static methods can be called using an object reference to an object of the class in which this method is defined.
3. Static methods are always public, because they are defined at class-level.
4. Static methods do not have direct access to non-static methods which are defined inside the same class.

Select an option to see the answer and solution.

What is the prototype of the default constructor?
public class Test { }

Select an option to see the answer and solution.

What is the output of the program?
class MyClass{
      MyClass(){
            System.out.print("one");
      }
      public void myMethod(){
            this();
            System.out.print("two");
      }
}
 
public class TestClass{
      public static void main(String args[]){
            MyClass obj = new MyClass();
            obj.myMethod();
      }
}

Select an option to see the answer and solution.

Determine output:
public class Test{
      public static void main(String args[]){
            MyClass obj = new MyClass();
            obj.val = 1;
            obj.call(obj);
            System.out.println(obj.val);
      }
}

class MyClass{
      public int val;
      public void call(MyClass ref){
            ref.val++;
      }
}

Select an option to see the answer and solution.

Determine output:
class MyClass{
	int i;
	int j;

	public MyClass(int i, int j){
		this.i = i;
		this.j = j;
	}

	public void call(){
		System.out.print("One");
	}
}

public class Test{
	public static void main(String args[]){
		MyClass m = new MyClass(); //line 1
		m.call(); //line 2
	}
}

Select an option to see the answer and solution.

public class MyClass{ }

For the above class(MyClass) what is the correct way of declaring constructor?

Select an option to see the answer and solution.

What is the output for the below code?
public class A{
      int add(int i, int j){
            return i+j;
      }
}
public class B extends A{
      public static void main(String argv[]){
            short s = 9;
            System.out.println(add(s,6));
      }
}

Select an option to see the answer and solution.

What will be the output?
public class Test{
      public static void main(String[] args){
            String value = "abc";
            changeValue(value);
            System.out.println(value);
      }

      public static void changeValue(String a){
            a = "xyz";
      }
}

Select an option to see the answer and solution.

What is the output for the below code?
public class Test{
      public static void printValue(int i, int j, int k){
            System.out.println("int");
      }

      public static void printValue(byte...b){
            System.out.println("long");
      }

      public static void main(String... args){
            byte b = 9;
            printValue(b,b,b);
      }
}

Select an option to see the answer and solution.

class A{
      A(String s){}

      A(){}
}

class B extends A{
      B(){}
      B(String s){
            super(s);
      }
      void test(){
            // insert code here
      }
}

Which of the below code can be insert at line 7 to make clean compilation ?

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 is a legal definition of a method named examveda assuming it throws IOException, and returns void. Also assume that the method does not take any arguments. Select the one correct answer.

Select an option to see the answer and solution.