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: 1Given the following piece of code:
public interface Guard
{
void doYourJob();
}
abstract public class Dog implements Guard
{
}
Which of the following statements is correct?
Option D
In Java, an interface can contain abstract methods, and a class that implements the interface normally has to provide implementations for those methods. However, there is an important exception when the implementing class is declared as an abstract class.
The interface Guard declares the method void doYourJob(); This method is implicitly public and abstract because methods declared in an interface without a body are abstract methods by default.
E.g.:
public interface Guard
{
void doYourJob();
}
abstract public class Dog implements Guard
{
}
class PoliceDog extends Dog
{
public void doYourJob()
{
System.out.println("Dog is performing its duty.");
}
}
class Example
{
public static void main(String args[])
{
PoliceDog dog = new PoliceDog();
dog.doYourJob();
}
}
Q: 2Which of the following statement is true about public and private members in Java?
Option D
In Java, access modifiers determine the visibility of class members. The two commonly used modifiers are public and private.
Private members are accessible only within the same class in which they are declared, they cannot be accessed from subclasses or other classes.
Public members are accessible from any other class, regardless of the package or subclass relationship.
MEMBER ACCESS RULE TABLE:
| ACCESS MODIFIER → ACCESS LOCATION ↓ | PUBLIC | PROTECTED | DEFAULT | PRIVATE |
|---|---|---|---|---|
| Same class. | Yes | Yes | Yes | Yes |
| Subclass in same package. | Yes | Yes | Yes | No |
| Other classes in same package. | Yes | Yes | Yes | No |
| Subclass in others package. | Yes | Yes | No | No |
| Non-subclasses in others package. | Yes | No | No | No |
Q: 3In Java, which mechanism helps in the naming as well as visibility control of the classes?
Option A
In Java, a package is a mechanism used to organize related classes and interfaces under a common name. Packages help in naming classes by providing a namespace and also help in controlling the visibility or access of classes and their members.
Q: 4Which statement about interfaces is correct?
Option B
In Java, an interface is a reference type that defines a contract for classes.
A class can implement an interface by using the implements keyword. One of the important features of interfaces is that a single class can implement multiple interfaces, which allows Java to support a form of multiple inheritance of type.
E.g.:
interface Printable
{
void print();
}
interface Showable
{
void show();
}
class TestExample implements Printable, Showable
{
public void print()
{
System.out.println("Print Method");
}
public void show()
{
System.out.println("Show Method");
}
public static void main(String args[])
{
TestExample obj = new TestExample();
obj.print();
obj.show();
}
}
OUTPUT
Print Method
Show Method
An interface cannot be instantiated directly because it does not represent a complete concrete object by itself. For example, the statement Printable obj = new Printable(); is invalid.
However, an interface reference can refer to an object of a class that implements that interface.
E.g.:
Printable obj = new TestExample();
Also, an interface extends another interface, not a class. A class uses extends to inherit from another class and implements to implement an interface.
Q: 5Which 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: 6A 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: 7Which 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: 8Choose 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: 9What is the default access level of an interface field?
Option C
In Java, every field declared inside an interface is automatically public, static, and final, even if these keywords are not explicitly written. Therefore, an interface field is a public constant.
E.g.:
interface Student
{
int AGE = 20;
}
The compiler treats the field approximately as:
interface Student
{
public static final int AGE = 20;
}
E.g.:
interface Student
{
int AGE = 20;
}
class TestExample
{
public static void main(String args[])
{
System.out.println(Student.AGE);
}
}
OUTPUT
20
Q: 10In Java ________ members are accessible only inside their own class and nowhere else.
Option A
In Java, a member declared as private can be accessed only within the class in which it is declared. It cannot be directly accessed from another class, even if that class is a subclass.
MEMBER ACCESS RULE TABLE:
| ACCESS MODIFIER → ACCESS LOCATION ↓ | PUBLIC | PROTECTED | DEFAULT | PRIVATE |
|---|---|---|---|---|
| Same class. | Yes | Yes | Yes | Yes |
| Subclass in same package. | Yes | Yes | Yes | No |
| Other classes in same package. | Yes | Yes | Yes | No |
| Subclass in others package. | Yes | Yes | No | No |
| Non-subclasses in others package. | Yes | No | No | No |
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.