What is a class in Java?
A
A variable B
An object C
A blueprint or template for creating objects D
A type of method
Analysis & Theory
A class defines the structure and behavior (fields and methods) that objects will have.
What is an object in Java?
A
A method of a class B
A variable inside a method C
An instance of a class D
A static block
Analysis & Theory
An object is a specific instance of a class with actual values assigned to its fields.
Which keyword is used to create an object in Java?
A
class B
object C
new D
create
Analysis & Theory
The `new` keyword is used to create an object in Java.
What is the output?
```java
class Car {
String color = "Red";
}
public class Test {
public static void main(String[] args) {
Car myCar = new Car();
System.out.println(myCar.color);
}
}```
A
Red B
color C
Car D
null
Analysis & Theory
The object `myCar` accesses the `color` field which is set to 'Red'.
What does a constructor do in a class?
A
Defines a static method B
Calls another method C
Initializes an object D
Creates a variable
Analysis & Theory
Constructors are special methods used to initialize newly created objects.
What is the default return type of a constructor?
A
int B
void C
String D
None (constructors do not have return types)
Analysis & Theory
Constructors do not have a return type, not even `void`.
What is the output?
```java
class Student {
int age = 20;
}
class Main {
public static void main(String[] args) {
Student s = new Student();
System.out.println(s.age);
}
}```
A
20 B
age C
Student D
0
Analysis & Theory
The object `s` is created and its `age` field is accessed, which is 20.
Can a class contain both fields and methods?
A
Yes B
No C
Only methods D
Only fields
Analysis & Theory
A class can contain both fields (variables) and methods (functions).
Which of these is the correct way to define a class in Java?
A
`public void class Car {}` B
`class Car {}` C
`Car = class {}` D
`define class Car {}`
Analysis & Theory
The correct syntax is `class Car {}`.
Which statement is true about objects?
A
Objects do not have memory B
Each object has its own copy of instance variables C
All objects share the same constructor only D
Objects are declared using `static`
Analysis & Theory
Each object has its own copy of the class’s instance variables unless declared `static`.