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: 1Read 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: 2What 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: 3Which 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: 4Which of the following is a valid constructor declaration for the class named as Employee in Java?
Option A
In Java, a Constructor is a special member of a class that is automatically invoked when an object is created. The most important rule for identifying a constructor is that its name must be exactly the same as the class name, and it must not have a return type, not even void.
In statement Employee(Employee e) {}, the name Employee matches the class name, and there is no return type before it. The parameter type Employee does not affect whether the declaration follows the basic constructor syntax.
class Employee
{
Employee()
{
System.out.println("Zero Argument Constructor Called");
}
Employee(Employee e)
{
System.out.println("Parameterized Constructor Called");
}
public static void main(String args[])
{
Employee obj1=new Employee();
Employee obj2 = new Employee(obj1);
}
}
OUTPUT
Zero Argument Constructor Called
Parameterized Constructor Called
Q: 5Which of the following is a correct statement about constructors in Java?
Option C
In Java, a constructor is a special block of code that is automatically executed when an object of a class is created. Its main purpose is to initialize the object, that is, to assign initial values to variables or run setup code.
class MyClass
{
MyClass() //This is a constructor.
{
System.out.println("Constructor called.");
}
void MyClass() //Not a constructor, this is a method.
{
System.out.println("This is a method.");
}
}
Q: 6What is the purpose of the ‘new’ keyword in Java?
Option C
In Java, the new keyword is used to create new objects at runtime. When a class is defined, it serves as a blueprint, but no memory is allocated until an object of that class is created. The new keyword allocates memory for the object on the heap and calls the constructor of the class to initialize it.
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.