Object Oriented Programming - I (3140705) MCQs

MCQs of Objects and Classes

Showing 21 to 30 out of 36 Questions
21.
What is true about Class.getInstance()?
(a) Class.getInstance calls the constructor
(b) Class.getInstance is same as new operator
(c) Class.getInstance needs to have matching constructor
(d) Class.getInstance creates object if class does not have any constructor
Answer:

Option (d)

22.
Which of these cannot be declared static?
(a) class
(b) object
(c) variable
(d) method
Answer:

Option (b)

23.
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:

Option (d)

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

Option (a)

25.
Determine output:
public class Test{
	int i = 34;
	public static void main(String args[]){
		Test t1 = new Test();
		Test t2 = new Test();
		t1.i = 65;
		System.out.print(t1.i);
		System.out.print(t2.i);
	}
}
(a) 34 34
(b) 65 34
(c) 65 65
(d) 34 65
Answer:

Option (b)

26.
What is the output for the below code ?
class A{
      int k;
      boolean istrue;
      static int p;
      public void printValue(){
            System.out.print(k);
            System.out.print(istrue);
            System.out.print(p);
      }
}

public class Test{
      public static void main(String argv[]){
            A a = new A();
            a.printValue();
      }
}
(a) 0 false 0
(b) 0 true 0
(c) 0 0 0
(d) Compile error - static variable must be initialized before use.
Answer:

Option (a)

27.
Determine output:
public class Test{
      int a = 10;
    
      public void method(int a){
            a += 1;
            System.out.println(++a);
      }
      public static void main(String args[]){
            Test t = new Test();
            t.method(3);
      }
}
(a) 4
(b) 5
(c) 12
(d) 11
Answer:

Option (b)

28.
What is the result of compiling and running the following code?
public class Tester{
	static int x = 4;
	int y = 9;  	
	public Tester(){
		System.out.print(this.x); // line 1
		printVariables();
	}
	public static void printVariables(){
		System.out.print(x); // line 2
		System.out.print(y); // line 3
	}
	public static void main(String... args) { // line 4
		new Tester();
	}
}
(a) Compile error at line 1 (static x must be only accessed inside static methods)
(b) Compile error at line 3 (static methods can't make reference to non-static variables)
(c) Compile error at line 4 (invalid argument type for method main)
(d) 49
Answer:

Option (b)

29.
What will be the output?
public class Test{
	static{
		int a = 5;
	}

	public static void main(String args[]){
		new Test().call();
	}

	void call(){
		this.a++;
		System.out.print(this.a);
	}
}
(a) Compile with error
(b) Runtime Exception
(c) 5
(d) 6
Answer:

Option (a)

30.
What will be the output of the following Java program?
    class main_class 
    {
        public static void main(String args[])
        {
            int x = 9;
            if (x == 9) 
            { 
                int x = 8;
                System.out.println(x);
            }
        } 
    }
(a) 9
(b) 8
(c) Compilation error
(d) Runtime error
Answer:

Option (c)

Showing 21 to 30 out of 36 Questions