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: 1The “this” reference is used in conjunction with ________ methods.
Option C
In Java, the this keyword refers to the current object of a class. It is used to access the current object instance variables, instance methods, and constructors.
Since this refers to a particular object, it can be used in non-static methods, which are associated with objects.
A static method belongs to the class rather than to a particular object. Therefore, the this reference cannot be used inside a static method because no current object is associated with that method call.
Q: 2Read the following code snippet and answer
class Rectangle
{
private int length;
private int breadth;
public int area=0;
public Rectangle()
{
this.length=0;
this.breadth=0;
this.area=0;
}
public Rectangle(int l, int b)
{
this.length=l;
this.breadth=b;
this.area=l*b;
}
public static void main(String a[])
{
Rectangle A = new Rectangle(20,5);
System.out.println(A.area);
}
}
Option B
In this program, the class Rectangle contains:
There are two constructors:
Default Constructor : Sets all values to 0.
Parameterized constructor : Assigns length=l, breadth=b, and calculates area=l*b.
When the statement Rectangle A=new Rectangle(20,5); is executed, the parameterized constructor is called because two arguments are passed. Inside this constructor, length is assigned the value 20, breadth is assigned 5, and area is calculated as l*b, which becomes 20*5 = 100.
After object creation, System.out.println(A.area); prints the value stored in the instance variable area, i.e., 100.
There is no compilation or runtime error because the variable area is properly declared as public int area and initialized.
Q: 3Which of the following is the correct statement to create an object of Data class?
Option A
In Java, an object is created using the new keyword followed by a constructor call. A variable is then used to store the reference to the newly created object.
Syntax:
ClassName refName = new ClassName();
E.g.:
class Data
{
void display()
{
System.out.println("Data Object Created");
}
}
class Example
{
public static void main(String args[])
{
Data d = new Data();
d.display();
}
}
OUTPUT
Data Object Created
Q: 4What does the following code do?
public class Example {
private int var;
public Example(int var) { this.var = var; }
}
Option C
In this code, the class Example has a private instance variable named var. The constructor Example(int var) receives a parameter with the same name. Inside the constructor, the statement this.var = var; is used to distinguish between the instance variable and the constructor parameter.
Here, this.var refers to the instance variable of the current object, while var refers to the parameter. Therefore, the code simply assigns the value passed into the constructor to the object's own variable.
Q: 5Which of the following statement(s) is/are correct regarding java programming?
I. Place a semicolon immediately after a method header
II. Define a method outside the braces of a class definition.
Option D
In Java, a method is a block of code that performs a particular task. Methods are normally defined inside the braces { } of a class. Java does not allow a normal method definition to exist independently outside a class.
A method header contains information such as the access modifier, return type, method name, and parameters.
public void displayMessage()
{
System.out.println("Hello, Suresh");
}
Q: 6What happens if a class contains no constructor declaration?
Option A
If a Java class does not contain any constructor declaration, the Java compiler automatically provides a default constructor. This constructor is a no-argument constructor, and it allows objects of that class to be created.
It is important to understand that this happens only when the class does not declare any constructor. If we declare even one constructor, the compiler does not automatically provide the default constructor.
E.g.:
class Student
{
Student(int age)
{
System.out.println(age);
}
public static void main(String args[])
{
Student s = new Student();
}
}
This produces a compilation error because the class has a constructor that requires an int argument, and the compiler will not additionally create a no-argument constructor.
Q: 7Which statement is true about method overloading?
Option B
In Java, Method Overloading means defining multiple methods in the same class with the same method name but different parameter lists. The parameter list can differ in the number, type, or order of parameters.
Remember, the method return type does not play any role in method overloading.
E.g.:
class MyCalculator
{
int add(int a,int b)
{
return a+b;
}
int add(int a,int b,int c)
{
return a+b+c;
}
double add(double a,double b)
{
return a+b;
}
public static void main(String args[])
{
MyCalculator calc = new MyCalculator();
System.out.println(calc.add(11,22));
System.out.println(calc.add(11,22,33));
System.out.println(calc.add(11.5,22.5));
}
}
OUTPUT
33
66
34.0
Method overloading is resolved by the compiler, based on the arguments supplied in the method call. Therefore, it is known as Compile-Time Polymorphism or Static Polymorphism.
Q: 8Which keyword refers to the current object?
Option A
In Java, the this keyword refers to the current object. It is mainly used inside instance methods and constructors when we need to refer to the object whose method or constructor is currently being executed.
E.g.:
class TestExample
{
int a = 10;
void show()
{
int a = 20;
System.out.println("Value : "+a);
System.out.println("Value : "+this.a);
}
public static void main(String args[])
{
new TestExample().show();
}
}
OUTPUT
Value : 20
Value : 10
Here, a refers to the local variable, while this.a explicitly refers to the instance variable of the current object.
Q: 9What is output of below given piece of code?
class TestExample
{
int a = 10;
void show()
{
int a = 20;
System.out.println(a);
}
public static void main(String args[])
{
new TestExample().show();
}
}
Option B
In Java, when a local variable and an instance variable have the same name, the local variable takes precedence within the method. This situation is called Variable Shadowing.
In the given code, the class contains an instance variable int a = 10;. But inside the show() method, another local variable with the same name is declared as int a = 20;.
When System.out.println(a); is executed inside the show() method, Java refers to the nearest variable in scope, which is the local variable a. Therefore, the value 20 is printed.
The instance variable a containing 10 still exists, but it is hidden (shadowed) by the local variable a inside the show() method. If we want to access the instance variable instead, we can use the this keyword.
E.g.:
class TestExample
{
int a = 10;
void show()
{
int a = 20;
System.out.println("Value : "+a);
System.out.println("Value : "+this.a);
}
public static void main(String args[])
{
new TestExample().show();
}
}
OUTPUT
Value : 20
Value : 10
Here, a refers to the local variable, while this.a explicitly refers to the instance variable of the current object.
Q: 10Which statement about constructors is correct?
Option C
In Java, a Constructor is a special member of a class that is used to initialize objects when they are created. A constructor has the same name as the class and does not have a return type, not even void. It is automatically called when an object is created using the new keyword.
E.g.:
class Student
{
Student()
{
System.out.println("Constructor Called");
}
public static void main(String args[])
{
Student s = new Student();
}
}
OUTPUT
Constructor Called
Note:
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.