3 - Object Oriented Programming Using Java

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Object Oriented Programming Using Java - MCQ

1. What is the mechanism by which one class acquires the properties (methods and fields) of
another class?
a) Inheritance
b) Polymorphism
c) Encapsulation
d) Abstraction
Answer: a) Inheritance

2. Which keyword is used to create an object in Java?


a) new
b) this
c) super
d) static
Answer: a) new

3. Which access specifier provides the widest accessibility in Java?


a) public
b) private
c) protected
d) default (package-private)
Answer: a) public

4. Which of the following is used to define a constant in Java?


a) final
b) static
c) const
d) constant
Answer: a) final

5. In Java, which keyword is used to refer to the current instance of the class?
a) this
b) super
c) self
d) me
Answer: a) this

6. Which keyword is used to prevent method overriding in Java?


a) static
b) final
c) abstract
d) override
Answer: b) final

7. Which of the following statements about abstract classes in Java is true?


a) Abstract classes cannot have constructors.
b) Abstract classes can be instantiated.
c) Abstract classes can only have abstract methods.
d) Abstract classes cannot have any methods.
Answer: a) Abstract classes cannot have constructors.

8. What is the purpose of the `super` keyword in Java?


a) To call the superclass constructor
b) To access the superclass's fields and methods
c) To create a new instance of a class
d) To indicate that a method is overridden
Answer: b) To access the superclass's fields and methods

9. Which of the following is true about method overloading in Java?


a) Method overloading occurs when two methods have the same name and parameters but
different return types.
b) Method overloading occurs when two methods have the same name and return type but
different parameters.
c) Method overloading is not allowed in Java.
d) Method overloading occurs when a subclass overrides a method of its superclass.
Answer: b) Method overloading occurs when two methods have the same name and return
type but different parameters.

10. What is the main advantage of using interfaces in Java?


a) Interfaces allow for multiple inheritance.
b) Interfaces can contain constructors.
c) Interfaces can have method implementations.
d) Interfaces allow for abstraction and provide a contract for implementing classes.
Answer: d) Interfaces allow for abstraction and provide a contract for implementing classes.

11.What is the purpose of the new keyword in Java?


A) To define a new type
B) To create a new variable
C) To create a new instance of a class
D) To allocate memory in the stack

Answer: C) To create a new instance of a class


12.Which of these access specifiers can be used for an interface?
A) Public
B) Protected
C) Private
D) All of the above

Answer: A) Public

13.What is polymorphism in Java?


A) The ability of a class to extend multiple classes
B) The ability of a class to implement multiple interfaces
C) The ability of Java to dynamically bind method calls at runtime
D) The ability of a single variable to hold multiple types of data

Answer: C) The ability of Java to dynamically bind method calls at runtime

14.Which keyword is used to inherit a class in Java?


A) super
B) this
C) extends
D) implements

Answer: C) extends

15.What does the @Override annotation indicate?


A) That a method is being reused from the parent class
B) That a method is overriding a method from its superclass
C) That a method is overloading another method in the same class
D) That a method cannot be overridden by subclasses

Answer: B) That a method is overriding a method from its superclass

16.Which of the following is not an OOP principle?


A) Encapsulation
B) Inheritance
C) Compilation
D) Polymorphism

Answer: C) Compilation
17.What is encapsulation in Java?
A) The process of wrapping code and data together into a single unit
B) The ability of a class to extend more than one class
C) A technique to define different methods of the same class
D) The mechanism of linking a function with an object

Answer: A) The process of wrapping code and data together into a single unit

18.Which of these is a correct way to instantiate an array in Java?


A) int arr[] = new int[5];
B) int arr[] = new int();
C) int[] arr = {1, 2, 3, 4, 5};
D) Both A and C

Answer: D) Both A and C

19.Which method can be defined only once in a program?


A) main method
B) finalize method
C) static method
D) abstract method

Answer: A) main method

20.What does the static keyword indicate?


A) That a method can be accessed without creating an instance of the class
B) That a variable or method belongs to the class, rather than an instance of it
C) That a method can change state
D) Both A and B

Answer: D) Both A and B

21.Which of the following is true about constructors in Java?


A) Constructors can have a return type.
B) Constructors can be inherited.
C) Constructors can be private.
D) Constructors can be abstract.
Answer: C) Constructors can be private.

22.What is the difference between an abstract class and an interface in Java?


A) An abstract class can have concrete methods, while an interface cannot.
B) An interface can have instance variables, while an abstract class cannot.
C) An abstract class can implement multiple interfaces, while an interface cannot.
D) An interface can have constructors, while an abstract class cannot.

Answer: A) An abstract class can have concrete methods, while an interface cannot.

23.Which keyword is used to prevent method overriding in Java?


A) final
B) static
C) private
D) abstract

Answer: A) final

24.What is the purpose of the "super" keyword in Java?


A) To call the superclass constructor
B) To call a method in the superclass
C) To refer to the current class instance
D) To access static variables

Answer: B) To call a method in the superclass

25.Which of the following is true about method overloading in Java?


A) Methods with the same name must have different return types.
B) Method overloading is determined at compile time.
C) Only static methods can be overloaded.
D) Constructors cannot be overloaded.

Answer: B) Method overloading is determined at compile time.

26.What is the output of the following code?

```java
class Test {
public static void main(String[] args) {
String str = "Hello";
str.concat(" World");
System.out.println(str);
}
}
```

A) Hello
B) World
C) Hello World
D) Compile-time error

Answer: A) Hello

27.Which of the following statements about interfaces in Java is true?


A) Interfaces can have instance variables.
B) Interfaces cannot extend other interfaces.
C) Interfaces can have private methods.
D) Interfaces can have constructors.

Answer: C) Interfaces can have private methods.

28.What is the purpose of the "instanceof" operator in Java?


A) To check if two objects are the same.
B) To compare two instances of the same class.
C) To check if an object is an instance of a particular class.
D) To cast an object to a superclass.

Answer: C) To check if an object is an instance of a particular class.

29.Which of the following statements is true about method overriding in Java?


A) Overridden methods must have the same return type.
B) Overridden methods can have a lower access modifier than the original method.
C) Overridden methods cannot throw checked exceptions that are broader than those thrown by
the original method.
D) Overridden methods cannot be declared final.

Answer: C) Overridden methods cannot throw checked exceptions that are broader than those
thrown by the original method.
30.What is the purpose of the "this" keyword in Java?
A) To refer to the current class instance
B) To call the superclass constructor
C) To create a new instance of a class
D) To access static variables

Answer: A) To refer to the current class instance

31. What is the main purpose of inheritance in Java?


a) Code reusability
b) Encapsulation of data
c) Polymorphism
d) Dynamic binding
Answer: a) Code reusability

32. What is the purpose of the "this" keyword in Java?


a) To call methods in the superclass
b) To create a new instance of a class
c) To access static variables
d) To refer to the current class instance
Answer: d) To refer to the current class instance

33. Which of the following is NOT a principle of Object-Oriented Programming?


a) Encapsulation
b) Inheritance
c) Abstraction
d) Procedural Programming
Answer: d) Procedural Programming

34. In Java, what does the "final" keyword signify when applied to a method?
a) The method cannot be overridden
b) The method cannot be accessed
c) The method cannot have parameters
d) The method cannot be called
Answer: a) The method cannot be overridden

35. Which of the following is true about interfaces in Java?


a) Interfaces can have constructors.
b) Interfaces can contain instance variables.
c) A class can implement multiple interfaces.
d) Interfaces can have method bodies.
Answer: c) A class can implement multiple interfaces.

36. What is the role of the "super" keyword in a constructor?


a) To call the superclass constructor
b) To call the subclass constructor
c) To initialize instance variables
d) To create a new instance of the class
Answer: a) To call the superclass constructor

37. What is the difference between composition and inheritance in Java?


a) Inheritance allows for code reuse, while composition allows for flexibility.
b) Composition allows for code reuse, while inheritance allows for flexibility.
c) Inheritance is achieved using interfaces, while composition is achieved using abstract
classes.
d) There is no difference; both achieve the same result.
Answer: a) Inheritance allows for code reuse, while composition allows for flexibility.

38. Which keyword is used to access the superclass's members within a subclass?
a) parent
b) super
c) this
d) extend
Answer: b) super

39. What is the purpose of the "static" keyword in Java?


a) To define global variables
b) To make variables and methods belong to the class, rather than instances of the class
c) To allocate memory on the heap
d) To prevent inheritance
Answer: b) To make variables and methods belong to the class, rather than instances of the
class

40. In Java, when does garbage collection occur?


a) When the program starts
b) When the program ends
c) When the program explicitly calls System.gc()
d) When the JVM determines that objects are no longer reachable
**Answer: d) When the JVM determines that objects are no longer reachable**

You might also like