Object Oriented Programming OOP Multiple Choice Questions MCQ - Samples for Exam

There are 60 questions in this post. Set 1 containd 30 and Set 2 also contains 30.
Solutions of each set is given after each set of questions.

Questions Set 1:

1.What is the output of the following code?

1: public class ArithmeticSample {
2: public static void main(String[] args) {
3: int x = 5 * 4 % 3;
4: System.out.println(x);
5: }}

A. 2
B. 3
C. 5
D. 6
E. The code will not compile because of line 3.

2. What is the output of the following code snippet?
3: int x = 4;
4: long y = x * 4 - x++;
5: if(y<10) System.out.println("Too Low");
6: else System.out.println("Just right");
7: else System.out.println("Too High");

A. Too Low
B. Just Right
C. Too High
D. Compiles but throws a NullPointerException.
E. The code will not compile because of line 6.
F. The code will not compile because of line 7.

3. Given the following classes, what is the maximum number of imports that can be removed and have the code still compile?
package aquarium; public class Water { }
package aquarium;
import java.lang.*;
import java.lang.System;
import aquarium.Water;
import aquarium.*;
public class Tank {
public void print(Water water) {
System.out.println(water); } }

A. 0
B. 1
C. 2
D. 3
E. 4
F. Does not compile.

4. What is the output of the following program?
1: public class WaterBottle {
2: private String brand;
3: private boolean empty;
4: public static void main(String[] args) {
5: WaterBottle wb = new WaterBottle();
6: System.out.print("Empty = " + wb.empty);
7: System.out.print(", Brand = " + wb.brand);
8: } }

A. Line 6 generates a compiler error.
B. Line 7 generates a compiler error.
C. There is no output.
D. Empty = false, Brand = null
E. Empty = false, Brand =
F. Empty = null, Brand = null

5. Given the following class in the file /my/directory/named/A/Bird.java:
INSERT CODE HERE
public class Bird { }
Which of the following replaces INSERT CODE HERE if we compile from /my/directory?
(Choose all that apply)
A. package my.directory.named.a;
B. package my.directory.named.A;
C. package named.a;
D. package named.A;
E. package a;
F. package A;
G. Does not compile.

6. What does the following code output?
1: public class Salmon {
2: int count;
3: public void Salmon() {
4: count = 4;
5: }
6: public static void main(String[] args) {
7: Salmon s = new Salmon();
8: System.out.println(s.count);
9: } }
A. 0
B. 4
C. Compilation fails on line 3.
D. Compilation fails on line 4.
E. Compilation fails on line 7.
F. Compilation fails on line 8.

7. What is the result of the following?
List<String> one = new ArrayList<String>();
one.add("abc");
List<String> two = new ArrayList<>();
two.add("abc");
if (one == two)
System.out.println("A");
else if (one.equals(two))
System.out.println("B");
else
System.out.println("C");
A. A
B. B
C. C
D. An exception is thrown.
E. The code does not compile.

8. Which of the following are true?
A. this() can be called from anywhere in a constructor.
B. this() can be called from any instance method in the class.
C. this.variableName can be called from any instance method in the class.
D. this.variableName can be called from any static method in the class.

9. Which modifier is implicitly applied to all interface methods?
A. protected
B. public
C. static
D. default

10. What is the output of the following code?
1: class Mammal {
2: public Mammal(int age) {
3: System.out.print("Mammal");
4: }
5: }
6: public class Platypus extends Mammal {
7: public Platypus() {
8: System.out.print("Platypus");
9: }
10: public static void main(String[] args) {
11: new Mammal(5);
12: }
13: }
A. Platypus
B. Mammal
C. PlatypusMammal
D. MammalPlatypus
E. The code will not compile because of line 8.
F. The code will not compile because of line 11.

11. Which statements are true for both abstract classes and interfaces?
A. All methods within them must be abstract.
B. Both can contain public static final variables.
C. Both can be extended using the extends keyword.
D. Both can contain default methods.

12. Which of the following is true about a concrete subclass?
A. A concrete subclass can be declared as abstract.
B. A concrete subclass must implement all inherited abstract methods.
C. A concrete subclass cannot be marked as final.
D. Abstract methods cannot be overridden by a concrete subclass.

13. Which exception will the following throw?
Object obj = new Integer(3);
String str = (String) obj;
System.out.println(str);
A. ArrayIndexOutOfBoundsException
B. ClassCastException
C. IllegalArgumentException
D. NumberFormatException
E. None of the above.

14. What will happen if you add the statement System.out.println(5 / 0); to a working
main() method?
A. It will not compile.
B. It will not run.
C. It will run and throw an ArithmeticException.
D. It will run and throw an IllegalArgumentException.
E. None of the above.

15. Which of the following is checked exception?
A. ArrayIndexOutOfBoundsException
B. IllegalArgumentException
C. IOException
D. NumberFormatException
E. Any exception that extends RuntimeException

16. Say that class Rodent has a child class Rat and another child class Mouse. Class Mouse has a child class PocketMouse. Examine the following:
Rodent rod;
Rat rat = new Rat();
Mouse mos = new Mouse();
PocketMouse pkt = new PocketMouse();

Which one of the following will cause a compiler error?
A. rod = rat;
B. rod = mos;
C. pkt = null;
D. pkt = rat;

17. Look at the following interface: 
interface Taxable { double taxRate = 0.06; double calculateTax(); }
Is the interface correct?  
A. No---because it contains a variable and interfaces cannot contain variables.
B. No---because the interface cannot contain a method that returns a value.
C. Yes--taxRate will automatically be a constant since it is in an interface.
D. Yes--the method body will automatically be filled in.

18. Which of the following statements are incorrect:
A. public members of class can be accessed by any code in the program.
B. private members of class can only be accessed by other members of the class.
C. private members of class can be inherited by a sub class, and become protected members in sub class.
D. protected members of a class can be inherited by a sub class, and become private members of the sub class.

19. Which of the following is true about an abstract method inherited into a class C?
A. It must be defined in C before C can be instantiated
B. It must be defined in C or C should be abstract
C. If it is defined in C and it is only method in C, then C would be a non-abstract class
D. All of above is true

20. What are valid arguments to the instanceof operator?
A. any primitive type
B. a class object and a class type 
C. boolean type only
D. class types only


21. Providing access to an object only through its member functions, while keeping the details private is called
A. Information hiding
B. Encapsulation
C. Inheritance
D. Modularity


22. In a student grading system, objects from different classes communicate with each other. These communications are known as ____________.
A. Inheritance
B. Message Passing
C. Polymorphism
D. SMS and MMS



23. Suppose the class Chair extends the class Furniture. Both classes are non-abstract. Which of the following assignments are legal?
I. Chair c = new Chair();
II. Furniture f = new Furniture();
III. Furniture f = new Chair();
IV. Object o = new Chair();

A. I and II only
B. II and III only
C. All are legal statements
D. I, II and III only

24. Which of following is right about abstract class
a. It cannot be extended.
b. The abstract class can be used to provide some implementation of the interface.
c. It must have at least one abstract method.
d. All of above

25. Which of the following statements is false?
A. A class can directly inherit from class Object.
B. It's often much more efficient to create a class by inheriting from a similar class than to create the class by writing every line of code the new class requires.
C. A class's instance variables are normally declared private to enforce good software engineering.
D. If the class you're inheriting from declares instance variables as private, the inherited class can access those instance variables directly.

Next 5 Questions refer to the following code. 
1. public class Desk 
2. { 
3.   private int length; 
4.   private int width; 
5.   private int height; 
6. 
7. public Desk(int length, int width, int h) 
8. { 
9.    length = length; 
10.  this.width = width; 
11.  height = this.h; 
12. } 
13. 
14. public int getLength() { 
15.    return this.length; 
16. } 
17.} 

26. What are the instance variables defined in this class? 
A. The three integers passed as parameters in the constructor, defined in line 7 
B. The three integers declared in lines 3-5 
C. There are none 
D. Only the ones where the this keyword is used in front of them 

27. What is line 9 doing? 
A. Assigning the value of the parameter length to the instance variable length 
B. Assigning the value of the instance variable length to the parameter length 
C. Assigning the value of the parameter length to itself, i.e. doing nothing useful 
D. Assigning the value of the instance variable length to itself, i.e. doing nothing useful 

28. Line 10 is attempting to assign the value of width, passed into the constructor, to the variable width in the Desk class. It was claimed that the this keyword is not needed. Which of the following is true? 
A. It is needed, but the left and right hand sides of the assignment should be swapped, so that the equation is: width = this.width; 
B. It is needed, because we need to differentiate between the width parameter and the width field (use this to refer to the field one), and the assignment is correct as written. 
C. It is needed, because we need to differentiate between the width parameter and the width field (use this to refer to the parameter one), and the assignment is correct as written. 
D. It is not needed, and should be rewritten as: width = width; 

29. Line 11 is attempting to assign the value of h to the variable height. It was claimed that there is an error in line 11. Which of the following is true? 
A. The error is there’s no field h in the Desk class, so can’t write this.h. It should be corrected to: this.height = h; 
B. The error is the same as specified in option A, but it should be corrected to the equation: h = height; 
C. The error is simply that the right and left hand sides of the assignment are switched. It should be corrected to: this.h = height; 
D. There is no error.

30. Assume that any compilation (i.e. compile time) errors in the above code are corrected. If an instance of this class is made with the instantiation: Desk d = new Desk(1,2,3); 
What is the return value of calling d.getLength()? The default value of integer field variables is 0. 
A. 0 
B. 1 
C. 2 
D. 3


Questions Set 2

Multiple choice questions. [30 Marks]

1. Overriding a method differs from overloading a method because:
A. Overloaded methods have the same signature.
B. Overridden methods have the same signature.
C. Both of the above. 
D. Neither of the above.

2. The default equals implementation of class Object determines:
A. whether two references refer to the same object in memory.
B. whether two references have the same type.
C. whether two objects have the same instance variables.
D. whether two objects have the same instance variable values.

3. Every class in Java, except ________, extends an existing class.
A. Integer.
B. Object.
C. String.
D. Class.

4. Which of the following statements is false?
A. A subclass is often larger than its superclass.
B. A superclass object is a subclass object.
C. The class following extends keyword in a class declaration is the direct superclass of the class being declared.
D. Java uses interfaces to provide the benefits of multiple inheritance.

5. When overriding a superclass method and calling the superclass version from the subclass method, failure to prefix the superclass method name with the keyword super and a dot (.) in the superclass method call causes ________.
A. a compile-time error.
B. a syntax error.
C. infinite recursion.
D. a runtime error.

6. Which of the following statements about abstract superclasses is true?
A. abstract superclasses may contain data.
B. abstract superclasses may not contain implementations of methods.
C. abstract superclasses must declare all methods as abstract.
D. abstract superclasses must declare all data members not given values as abstract.

7. Assigning a subclass reference to a superclass variable is safe ________.
A. because the subclass object has an object of its superclass.
B. because the subclass object is an object of its superclass.
C. only when the superclass is abstract.
D. only when the superclass is concrete.

8. Consider classes A, B and C, where A is an abstract superclass, B is a concrete class that inherits from A and C is a concrete class that inherits from B. Class A declares abstract method originalMethod, implemented in class B. Which of the following statements is true of class C?
A. Method originalMethod cannot be overridden in class C—once it has been implemented in concrete class B, it is implicitly final.
B. Method originalMethod must be overridden in class C, or a compilation error will occur.
C. If method originalMethod is not overridden in class C but is called by an object of class C, an error occurs.
D. None of the above.

9. Consider the abstract superclass below:
public abstract class Foo
{
   private int a;
   public int b;

   public Foo(int aVal, int bVal){
      a = aVal;
      b = bVal;
   }

   public abstract int calculate();
Any concrete subclass that extends class Foo:
A. Must implement a method called calculate.
B. Will not be able to access the instance variable a.
C. Neither (a) nor (b).
D. Both (a) and (b).

10. Which of the following statements is true?
A. The throw statement is used to throw an exception.
B. The throw statement is used to specify that a method will throw an exception.
C. The throw statement is used to access an exception parameter.
D. All of the above.

11. In the catch block below, what is e?
catch (ArithmeticException e) 
   { 
      System.out.println(e); 
   } 
A. The type of the exception being caught.
B. The name of catch block’s exception parameter.
C. A finally block.
D. An exception handler.

12. All exception classes inherit, either directly or indirectly, from ________.
A. class Error.
B. class RuntimeException.
C. class Throwable.
D. None of the above.

13. When must a program explicitly use the this reference?
A. Accessing a private variable.
B. Accessing a public variable.
C. Accessing a local variable.
D. Accessing an instance variable that is shadowed by a local variable.

14. What will be the output of following code segment?
class A {
        int i;
        void display() {
            System.out.println(i);
        }
    }    
    class B extends A {
        int j;
        void display() {
            System.out.println(j);
        }
    }    
    class inheritance_demo {
        public static void main(String args[]) {
            B obj = new B();
            obj.i=1;
            obj.j=2;   
            obj.display();     
        }
   }
A. 0
B. 1
C. 2
D. Compilation Error

15. Which of the following statements is true?
A. The code in a finally block is executed only if an exception occurs.
B. The code in a finally block is executed only if an exception does not occur.
C. The code in a finally block is executed only if there are no catch blocks.
D. None of the above are true.

16. What is the output of this program?
    class A {
        public int i;
        protected int j;
    }    
    class B extends A {
        int j;
        void display() {
            super.j = 3;
            System.out.println(i + " " + j);
        }
    }    
    class Output {
        public static void main(String args[])
        {
            B obj = new B();
            obj.i=1;
            obj.j=2;   
            obj.display();     
        }
   }
A. 1 2
B. 2 1
C. 1 3
D. 3 1

17. What is the error in the following class definitions?
abstract class XY{
  abstract sum(int x, int y){    }
}
A. Class header is not defined properly
B. Constructor is not defined
C. Method is not defined properly
D. No error.

18. Which of the following is correct syntax for defining a new class Movie based on the superclass Multimedia?  
A. class Movie isa Multimedia { //additional definitions go here }
B. class Movie implements Multimedia { //additional definitions go here }
C. class Movie defines Multimedia { //additional definitions go here }
D. class Movie extends Multimedia { //additional definitions go here }

19. Inheritance is also known as the 
A. knows-a relationship.
B. has-a relationship.
C. uses-a relationship.
D. is-a relationship.

20. Which is true?
A. "X extends Y" is correct if and only if X is a class and Y is an interface
B. "X extends Y" is correct if and only if X is an interface and Y is a class
C. "X extends Y" is correct if X and Y are either both classes or both interfaces
D. "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces

21. What will be the output of this program?
class A {
int i = 10;
}
class B extends A{
int i = 20;
}
public class MainClass{
public static void main(String[] args){
A a = new B();
System.out.println(a.i);
}
}
A. 10
B. 20
C. Both
D. Neither 

22. Which of following is/are true
i. If you make any method as final, you cannot override it.
ii. If you make any class as final, you cannot extend it.
iii. If you make any method as final, it is static by default.
iv. If you make any class as final, you cannot instantiate it.
A. Only i and ii
B. Only ii and iii
C. Only iii and iv
D. None is true

23. Which of following is/are true for checked exceptions:
A. The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions
B. The classes that extend Throwable class are known as checked exceptions
C. Examples are ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException 
D. Checked exceptions are checked at run-time.

24. If we divide any number by zero, there occurs a/ an 
A. ArithmeticException.
B. DivideByZeroException
C. InfinitException
D. It causes a compile time error not an Exception

25. Declaring overridden methods with the @Override annotation:
A. Ensures at compilation time that you defined their signatures correctly
B. Is necessary to override a superclass method in base class, compiler will give an error message otherwise.
C. Ensures at run time that you defined their signatures correctly
D. It is your choice; if you use the annotation, the method is overridden, if you don’t, method with same signature is made in the subclass class but is not overridden.

26. When a subclass constructor calls its superclass constructor, what happens if the superclass’s constructor does not assign a value to an instance variable?
A. A syntax error occurs.
B. A compile-time error occurs.
C. A run-time error occurs.
D. The program compiles and runs because the instance variables are initialized to their default values.

27. Which of the following statements is false?
A. A class can directly inherit from class Object.
B. It's often much more efficient to create a class by inheriting from a similar class than to create the class by writing every line of code the new class requires.
C. If the class you're inheriting from declares instance variables as private, the inherited class can access those instance variables directly.
D. A class's instance variables are normally declared private to enforce good software engineering.

28. Which superclass members are inherited by all subclasses of that superclass?
A. private instance variables and methods.
B. protected instance variables and methods.
C. private constructors.
D. protected constructors.

29. Which of the following methods are overloaded with respect to one another?
public int max (int a, int b) { … }
public double max (double a, double b) { … }
public int max (int a, int b, int c) { … }
public double max (double a, double b, double c) { … }
A. A and B are overloaded; C and D are overloaded.
B. A and C are overloaded; B and D are overloaded.
C. A, B and C are overloaded.
D. All four methods are overloaded.

30. Which keyword is used to specify that a class will define the methods of an interface?
A. uses
B. implements
C. defines
D. extends





Comments