0% found this document useful (0 votes)
10 views13 pages

Wings 1 Java Spring MCQ

The document contains a series of Java programming questions and answers covering various concepts such as inheritance, encapsulation, polymorphism, and exception handling. Each question is followed by multiple-choice options and the correct answer is provided. The content is designed to test knowledge of Java syntax and object-oriented programming principles.

Uploaded by

prayagdev109
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views13 pages

Wings 1 Java Spring MCQ

The document contains a series of Java programming questions and answers covering various concepts such as inheritance, encapsulation, polymorphism, and exception handling. Each question is followed by multiple-choice options and the correct answer is provided. The content is designed to test knowledge of Java syntax and object-oriented programming principles.

Uploaded by

prayagdev109
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Which keyword is used to define a subclass in Java?

A) super
B) subclass
C) extends
D) implements
Answer: C) extends
Explanation: In Java, the extends keyword is used to indicate that a class is a subclass of
another class.
What is the output of the following Java code snippet?
java

class Parent {
void display() {
System.out.println("Parent's display method");
}
}

class Child extends Parent {


void display() {
System.out.println("Child's display method");
}
}

public class Main {


public static void main(String[] args) {
Parent obj = new Child();
obj.display();
}
}
A) Parent's display method
B) Child's display method
C) Compilation Error
D) Runtime Error
Answer: B) Child's display method
Explanation: Since the reference variable is of type Parent but the object being referred to is of
type Child, the overridden method in Child class is invoked.
Which of the following is not a principle of object-oriented programming (OOP)?
A) Inheritance
B) Encapsulation
C) Abstraction
D) Structuring
Answer: D) Structuring
Explanation: Structuring is a broader concept that encompasses various programming
paradigms, including procedural programming, and is not specific to OOP.
What is the main purpose of using the super keyword in Java?
A) To access superclass variables and methods
B) To instantiate objects
C) To implement interfaces
D) To define abstract methods
Answer: A) To access superclass variables and methods
Explanation: The super keyword in Java is used to refer to the superclass members (variables
and methods) from within a subclass.
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 must implement all methods declared abstract.
D) Abstract classes can only have abstract methods.
Answer: A) Abstract classes cannot have constructors.
Explanation: Abstract classes can have constructors, but they cannot be directly instantiated.
They are meant to be subclassed.
What does the final keyword do when applied to a method in Java?
A) Prevents the method from being overridden
B) Prevents the method from being inherited
C) Prevents the method from being accessed outside the class
D) Prevents the method from being called
Answer: A) Prevents the method from being overridden
Explanation: When a method is declared as final, it cannot be overridden by subclasses.
What is the output of the following Java code snippet?
java

interface Animal {
void eat();
}

class Dog implements Animal {


public void eat() {
System.out.println("Dog is eating");
}
}

public class Main {


public static void main(String[] args) {
Animal animal = new Dog();
animal.eat();
}
}
A) Compilation Error
B) Runtime Error
C) Dog is eating
D) Animal is eating
Answer: C) Dog is eating
Explanation: The eat() method in Dog class overrides the eat() method of the Animal interface,
and hence the output is "Dog is eating".
Which of the following is a correct way to achieve encapsulation in Java?
A) Making all variables public
B) Making all variables private and providing public getter and setter methods
C) Making all variables protected
D) Not declaring any access modifiers for variables
Answer: B) Making all variables private and providing public getter and setter methods
Explanation: Encapsulation in Java involves hiding the internal state of an object and providing
access to it only through public methods.
What is the purpose of the toString() method in Java?
A) To convert a string to an object
B) To compare two objects for equality
C) To provide a string representation of an object
D) To initialize an object
Answer: C) To provide a string representation of an object
Explanation: The toString() method returns a string representation of an object, which is helpful
for debugging and printing object information.
Which of the following is true regarding constructors in Java?
A) Constructors can be inherited.
B) Constructors can be abstract.
C) Constructors can return a value.
D) Constructors have the same name as the class.
Answer: D) Constructors have the same name as the class.
Explanation: Constructors in Java have the same name as the class and are used to initialize
objects of that class.

User
create 50 questions based on the above
ChatGPT
Creating 50 unique questions based on the provided concepts may lead to redundancy, so I'll
diversify the questions to cover a wide range of topics in Java and object-oriented programming
(OOP). Let's begin:

Which keyword is used to define a subclass in Java?


A) super
B) subclass
C) extends
D) implements
Answer: C) extends
What is the output of the following Java code snippet?
java

class Parent {
void display() {
System.out.println("Parent's display method");
}
}

class Child extends Parent {


void display() {
System.out.println("Child's display method");
}
}

public class Main {


public static void main(String[] args) {
Parent obj = new Child();
obj.display();
}
}
A) Parent's display method
B) Child's display method
C) Compilation Error
D) Runtime Error
Answer: B) Child's display method
Which of the following is not a principle of object-oriented programming (OOP)?
A) Inheritance
B) Encapsulation
C) Abstraction
D) Structuring
Answer: D) Structuring
What is the main purpose of using the super keyword in Java?
A) To access superclass variables and methods
B) To instantiate objects
C) To implement interfaces
D) To define abstract methods
Answer: A) To access superclass variables and methods
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 must implement all methods declared abstract.
D) Abstract classes can only have abstract methods.
Answer: A) Abstract classes cannot have constructors.
What does the final keyword do when applied to a method in Java?
A) Prevents the method from being overridden
B) Prevents the method from being inherited
C) Prevents the method from being accessed outside the class
D) Prevents the method from being called
Answer: A) Prevents the method from being overridden
What is the output of the following Java code snippet?
java

interface Animal {
void eat();
}

class Dog implements Animal {


public void eat() {
System.out.println("Dog is eating");
}
}

public class Main {


public static void main(String[] args) {
Animal animal = new Dog();
animal.eat();
}
}
A) Compilation Error
B) Runtime Error
C) Dog is eating
D) Animal is eating
Answer: C) Dog is eating
Which of the following is a correct way to achieve encapsulation in Java?
A) Making all variables public
B) Making all variables private and providing public getter and setter methods
C) Making all variables protected
D) Not declaring any access modifiers for variables
Answer: B) Making all variables private and providing public getter and setter methods
What is the purpose of the toString() method in Java?
A) To convert a string to an object
B) To compare two objects for equality
C) To provide a string representation of an object
D) To initialize an object
Answer: C) To provide a string representation of an object
Which of the following is true regarding constructors in Java?
A) Constructors can be inherited.
B) Constructors can be abstract.
C) Constructors can return a value.
D) Constructors have the same name as the class.
Answer: D) Constructors have the same name as the class.
What is the difference between == and .equals() method in Java?
A) Both are used to compare primitive data types.
B) == compares references, while .equals() compares values for objects.
C) == compares values, while .equals() compares references for objects.
D) .equals() is used for primitive data types, while == is used for objects.
Answer: B) == compares references, while .equals() compares values for objects.
Which of the following statements about interfaces in Java is true?
A) Interfaces can contain instance variables.
B) A class can implement multiple interfaces using the extends keyword.
C) All methods in an interface must be declared as abstract.
D) Interfaces can have constructors.
Answer: B) A class can implement multiple interfaces using the extends keyword.
What is the purpose of the static keyword in Java?
A) It is used to create static classes.
B) It is used to define constants.
C) It is used to create class-level variables and methods.
D) It is used to prevent inheritance.
Answer: C) It is used to create class-level variables and methods.
Which of the following is true about method overloading in Java?
A) Method overloading occurs when a subclass overrides a superclass method.
B) Method overloading allows methods with different names to have the same signature.
C) Method overloading allows methods with the same name but different parameters in the
same class.
D) Method overloading allows methods with different return types.
Answer: C) Method overloading allows methods with the same name but different parameters in
the same class.
What is the purpose of the this keyword in Java?
A) It refers to the current instance of the class.
B) It is used to call superclass constructors.
C) It is used to call static methods.
D) It is used to access instance variables of the superclass.
Answer: A) It refers to the current instance of the class.
What happens when an exception is thrown but not caught in Java?
A) The program continues execution without any interruption.
B) The program terminates abnormally.
C) The program prompts the user to handle the exception.
D) The program automatically handles the exception.
Answer: B) The program terminates abnormally.
What is the purpose of the default keyword in Java interfaces?
A) It defines a default implementation for a method in the interface.
B) It specifies the default access modifier for variables.
C) It specifies the default package for classes.
D) It is used to define default constructors.
Answer: A) It defines a default implementation for a method in the interface.
Which of the following is not a valid access modifier in Java?
A) private
B) default
C) protected
D) package-private
Answer: B) default
What is the purpose of the break statement in Java?
A) It is used to exit a loop or switch statement.
B) It is used to skip the current iteration of a loop.
C) It is used to define a case in a switch statement.
D) It is used to define the default case in a switch statement.
Answer: A) It is used to exit a loop or switch statement.
What does the instanceof operator do in Java?
A) It checks if two objects are equal.
B) It checks if an object is an instance of a particular class.
C) It converts an object to an instance of a class.
D) It checks if an object is null.
Answer: B) It checks if an object is an instance of a particular class.
Which of the following is true about wrapper classes in Java?
A) Wrapper classes are used to wrap primitive data types.
B) Wrapper classes cannot be used in collections.
C) Wrapper classes are immutable.
D) Wrapper classes cannot be used in exception handling.
Answer: A) Wrapper classes are used to wrap primitive data types.
What is the output of the following Java code snippet?
java

String str1 = "hello";


String str2 = "hello";
System.out.println(str1 == str2);
A) true
B) false
C) Compilation Error
D) Runtime Error
Answer: A) true
Which of the following is true about the StringBuilder class in Java?
A) StringBuilder is immutable.
B) StringBuilder is synchronized.
C) StringBuilder is faster than StringBuffer.
D) StringBuilder is a subclass of String.
Answer: C) StringBuilder is faster than StringBuffer.
What is the purpose of the transient keyword in Java?
A) It is used to declare a variable constant.
B) It is used to prevent a variable from being serialized.
C) It is used to prevent a variable from being accessed outside the class.
D) It is used to declare a variable as non-final.
Answer: B) It is used to prevent a variable from being serialized.
Which of the following is true about the enum keyword in Java?
A) Enums cannot have constructors.
B) Enums can implement interfaces.
C) Enums can extend abstract classes.
D) Enums can have instance variables.
Answer: B) Enums can implement interfaces.
What is the purpose of the instanceof operator in Java?
A) It is used to check if an object is an instance of a particular class.
B) It is used to compare two objects for equality.
C) It is used to access the superclass of an object.
D) It is used to perform type casting.
Answer: A) It is used to check if an object is an instance of a particular class.
What is the output of the following Java code snippet?
java

class A {
void display() {
System.out.println("A's display method");
}
}

class B extends A {
void display() {
System.out.println("B's display method");
}
}

class C extends B {
void display() {
System.out.println("C's display method");
}
}

public class Main {


public static void main(String[] args) {
A obj = new C();
obj.display();
}
}
A) A's display method
B) B's display method
C) C's display method
D) Compilation Error
Answer: C) C's display method
Which of the following is true about the throws keyword in Java?
A) It is used to declare an exception within a method.
B) It is used to catch an exception.
C) It is used to propagate an exception to the caller.
D) It is used to handle an exception.
Answer: A) It is used to declare an exception within a method.
What is the purpose of the finally block in Java exception handling?
A) It is executed if an exception occurs.
B) It is executed after the try block, regardless of whether an exception occurs.
C) It is executed before the try block.
D) It is executed before the catch block.
Answer: B) It is executed after the try block, regardless of whether an exception occurs.
Which of the following is true about method overriding in Java?
A) Method overriding occurs when a subclass provides a specific implementation of a method
that is already defined in its superclass.
B) Method overriding allows a subclass to hide the implementation of a method in its
superclass.
C) Method overriding allows a subclass to access the private methods of its superclass.
D) Method overriding is not allowed in Java.
Answer: A) Method overriding occurs when a subclass provides a specific implementation of a
method that is already defined in its superclass.
What is the output of the following Java code snippet?
java

String str1 = "hello";


String str2 = new String("hello");
System.out.println(str1 == str2);
A) true
B) false
C) Compilation Error
D) Runtime Error
Answer: B) false
What is the purpose of the this keyword in Java constructors?
A) It refers to the superclass.
B) It refers to the current instance of the class.
C) It refers to the current class.
D) It refers to the parent class.
Answer: B) It refers to the current instance of the class.
Which of the following is true about the super keyword in Java?
A) It is used to call a superclass constructor.
B) It is used to call a static method.
C) It is used to access a superclass variable.
D) It is used to define a superclass.
Answer: A) It is used to call a superclass constructor.
What is the output of the following Java code snippet?
java

int x = 5;
int y = 2;
System.out.println(x / y);
A) 2
B) 2.5
C) 2.0
D) Compilation Error
Answer: A) 2
Which of the following is true about inheritance in Java?
A) A class can inherit from multiple classes.
B) Inheritance allows a subclass to access private members of its superclass.
C) Inheritance is a way to achieve code reusability and polymorphism.
D) Inheritance is not supported in Java.
Answer: C) Inheritance is a way to achieve code reusability and polymorphism.
What is the purpose of the interface keyword in Java?
A) It is used to declare a class as an interface.
B) It is used to define a class that cannot be instantiated.
C) It is used to define a blueprint of a class.
D) It is used to define a method that must be implemented by a class.
Answer: D) It is used to define a method that must be implemented by a class.
What is the output of the following Java code snippet?
java

String str = "hello";


System.out.println(str.charAt(0));
A) h
B) e
C) l
D) o
Answer: A) h
Which of the following is true about polymorphism in Java?
A) Polymorphism allows a method to have multiple signatures.
B) Polymorphism is achieved through method overloading but not method overriding.
C) Polymorphism allows objects of different types to be treated as objects of a common
superclass.
D) Polymorphism is not supported in Java.
Answer: C) Polymorphism allows objects of different types to be treated as objects of a common
superclass.
What is the output of the following Java code snippet?
java

int[] arr = {1, 2, 3, 4, 5};


System.out.println(arr.length);
A) 1
B) 5
C) Compilation Error
D) Runtime Error
Answer: B) 5
Which of the following is true about method overriding in Java?
A) Method overriding occurs when a subclass provides a specific implementation of a method
that is already defined in its superclass.
B) Method overriding allows a subclass to hide the implementation of a method in its
superclass.
C) Method overriding allows a subclass to access the private methods of its superclass.
D) Method overriding is not allowed in Java.
Answer: A) Method overriding occurs when a subclass provides a specific implementation of a
method that is already defined in its superclass.
What is the output of the following Java code snippet?
java

String str = "hello";


System.out.println(str.substring(2));
A) hel
B) ello
C) llo
D) lo
Answer: C) llo
Which of the following is true about the switch statement in Java?
A) The switch statement can only be used with integers.
B) The switch statement can only be used with strings.
C) The switch statement can have multiple default cases.
D) The switch statement can have fall-through behavior.
Answer: D) The switch statement can have fall-through behavior.
What is the output of the following Java code snippet?
java

int x = 5;
System.out.println(x++);
A) 4
B) 5
C) 6
D) Compilation Error
Answer: B) 5
Which of the following is true about the for-each loop in Java?
A) The for-each loop can only be used with arrays.
B) The for-each loop can only be used with collections.
C) The for-each loop guarantees the order of iteration.
D) The for-each loop uses an index variable to iterate.
Answer: C) The for-each loop guarantees the order of iteration.
What is the output of the following Java code snippet?
java

int[][] arr = {{1, 2, 3}, {4, 5, 6}};


System.out.println(arr[1][2]);
A) 1
B) 2
C) 3
D) 6
Answer: D) 6
Which of the following is true about the break statement in Java?
A) The break statement is used to skip the current iteration of a loop.
B) The break statement is used to exit a loop or switch statement.
C) The break statement is used to define a case in a switch statement.
D) The break statement is used to define the default case in a switch statement.
Answer: B) The break statement is used to exit a loop or switch statement.
What is the purpose of the continue statement in Java?
A) It is used to skip the current iteration of a loop.
B) It is used to exit a loop or switch statement.
C) It is used to define a case in a switch statement.
D) It is used to define the default case in a switch statement.
Answer: A) It is used to skip the current iteration of a loop.
What is the output of the following Java code snippet?
java

int x = 10;
if (x > 5) {
System.out.println("x is greater than 5");
} else {
System.out.println("x is less than or equal to 5");
}
A) x is greater than 5
B) x is less than 5
C) x is less than or equal to 5
D) Compilation Error
Answer: A) x is greater than 5
What is the output of the following Java code snippet?
java

for (int i = 0; i < 5; i++) {


if (i == 3) {
continue;
}
System.out.print(i + " ");
}
A) 0 1 2
B) 0 1 2 3
C) 0 1 2 4
D) Compilation Error
Answer: C) 0 1 2 4
Which of the following is true about the try-with-resources statement in Java?
A) The try-with-resources statement is used to catch exceptions.
B) The try-with-resources statement is used to create resources that need to be closed after
execution.
C) The try-with-resources statement guarantees that the resources will be closed even if an
exception occurs.
D) The try-with-resources statement is used to declare checked exceptions.
Answer: C) The try-with-resources statement guarantees that the resources will be closed even
if an exception occurs.

You might also like