This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1Which of the following statements is/are true regarding Java?
(A) Constants that cannot be changed are declared using the ‘static’ keyword.
(B) A class can only inherit one class that can implement multiple interfaces.
Option B
In Java, constants that cannot be changed are declared using final, often combined with static for class-level constants, example, static final int P = 33;. Simply using static does not make a variable constant.
Java supports single inheritance for classes, meaning a class can extend only one superclass. However, a class can implement multiple interfaces, allowing it to inherit behavior from multiple sources like multiple inheritance.
class Super {}
interface A {}
interface B {}
class Sub extends Super implements A, B
{
}
Q: 2A pure abstract class where all methods are without method body is generally called as
Option C
In Object-Oriented Programming (OOP), a pure abstract class is a class where all methods are abstract, meaning they do not have any method body and must be implemented by other classes.
An Interface is a special type of class that contains only abstract methods. It provides a blueprint for other classes.
Any class that implements an interface must provide implementations for all its methods. Therefore, a pure abstract class is generally called an interface.
Syntax:
interface InterfaceName
{
Return_Type method1();
Return_Type method2();
}
E.g.:
interface Shape
{
void draw();
}
class Circle implements Shape
{
public void draw()
{
System.out.println("Drawing Circle");
}
}
class Driver
{
public static void main(String args[])
{
Circle myCircle=new Circle();
myCircle.draw();
}
}
Q: 3Which of the following is the correct way to create an abstract class in Java?
Option C
In Java, an abstract class must be declared using the abstract keyword before the class name, and any abstract method inside it must also be declared with the abstract keyword and cannot have a method body.
Q: 4Choose the correct statement with respect to interfaces:
(i) Java does not support "multiple inheritance" however, it can be achieved by using interfaces.
(ii) Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.
(iii) To implement multiple interfaces, separate them with a comma (,).
Option D
Java does not support multiple inheritance with classes, i.e., a class cannot extend more than one class. However, Java allows a class to implement multiple interfaces. This provides a way to achieve multiple inheritance in Java.
When a class implements an interface, it must provide concrete definitions of all abstract methods defined in the interface. If the class does not implement all methods, it must be declared as abstract.
A class can implement multiple interfaces by separating them with a comma in the implements clause.
class Demo implements MyInterfaceP, MyInterfaceQ {
// Implementation of methods from both interfaces
}
interface MyInterfaceP {
void methodOne();
}
interface MyInterfaceQ {
void methodTwo();
}
class MyClass implements MyInterfaceP, MyInterfaceQ {
public void methodOne() {
System.out.println("Method One from MyInterfaceP implemented.");
}
public void methodTwo() {
System.out.println("Method Two from MyInterfaceQ implemented.");
}
}
public class Driver {
public static void main(String args[]) {
MyClass obj = new MyClass();
obj.methodOne();
obj.methodTwo();
}
}
Q: 5In OOPS concepts, how can you access a protected method of class A from class B outside the package?
Option B
In Object-Oriented Programming, protected members have a specific access rule:
If class A has a protected method and class B is in a different package, then class B cannot access that method directly. To access it, class B must inherit (extend) class A. Only through inheritance can class B access the protected method from outside the package.
Package A (Class A)
package mypack1;
public class A
{
protected void show()
{
System.out.println("Protected Method of Class A");
}
}
Package B (Class B inherits A)
package mypack2;
import mypack1.A;
public class B extends A
{
public static void main(String[] args)
{
B obj=new B();
obj.show(); // Accessing protected method through inheritance.
}
}
Q: 6Which of the following is an abstract method?
Option C
An abstract method is a method that is declared without a body and must be implemented by a subclass. It is used inside an abstract class or interface in Java.
Syntax:
public abstract void abc();
Q: 7In Java, the final keyword can be used with which of the following?
I. Variables
II. Exceptions
III. Classes
Option B
In Java, the final keyword is a non‑access modifier used to restrict changes. It can be applied to:
However, final is not used with exceptions in Java. Exceptions are handled with try, catch, throw, throws, etc.
Q: 8Which statement is true about an Interface in Java?
Option D
The abstract class is used for achieving partial abstraction. Unlike abstract class an interface is used for full abstraction.
Interface looks like a class but it is not a class. An interface can have methods and variables just like the class but the methods declared in interface are by default abstract (only method signature, no body). Also, the variables declared in an interface are public, static and final by default.
A class extends another class, an interface extends another interface, but a class implements an interface.
A class can implement more than one interface which is one of the key features of interfaces in Java.
interface MyInterface1
{
void fun1();
}
interface MyInterface2
{
void fun2();
}
class Demo implements MyInterface1,MyInterface2
{
public void fun1()
{
System.out.println("Inside fun1() Method");
}
public void fun2()
{
System.out.println("Inside fun2() Method");
}
void fun3()
{
System.out.println("Inside fun3() Method");
}
}
class InterfaceExample
{
public static void main(String args[])
{
Demo obj1=new Demo();
obj1.fun1();
obj1.fun2();
obj1.fun3();
MyInterface1 obj2=new Demo();
obj2.fun1();
//obj2.fun2(); Error.
//obj2.fun3(); Error.
MyInterface2 obj3=new Demo();
//obj3.fun1(); Error.
obj3.fun2();
//obj3.fun3(); Error.
}
}
OUTPUT
Inside fun1() Method
Inside fun2() Method
Inside fun3() Method
Inside fun1() Method
Inside fun2() Method
Q: 9What is the access specifier used to make members of a class accessible only within the same package?
Option C
In Java, access specifiers control the visibility of class members. To make members accessible only within the same package, we use package-private (default) access.
Q: 10Listed below are the properties of an interface. State which of them are true?
(I) Interface by default is an abstract class.
(II) Interface methods can be final or static.
(III) Interface methods cannot be final or static.
(IV) The methods of an interface are by default public and abstract.
Option D
In object-oriented programming (Java), an interface is a reference type that is used to achieve abstraction.
Q: 11Which keyword is used in Java to implement inheritance?
Option A
In Java, extends keyword is used for class inheritance where a subclass inherits properties and methods from a superclass. Syntax for inheritance in Java is class Child extends Parent {}.
You have reached the end of this topic. Continue learning with the next topic below.
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.