Question bank

Class 12

1 subject · 351 questions

Refine

All questions

351 results
181

An object is:

MCQ Easy
a. A class definition
b. An instance of a class
c. A primitive data type
d. A method with no body
Information Technology (802) · Class 12 · Sep 26, 2026
Show answer
An instance of a class
Explanation: An object is an instance of a class, populated with specific data for its data members.
182

In a class Book with data members Title, Author, Publisher and methods display() and getPrice(), the data members are also called:

MCQ Easy
a. Method members
b. Data members (attributes)
c. Constructors
d. Access modifiers
Information Technology (802) · Class 12 · Sep 26, 2026
Show answer
Data members (attributes)
Explanation: Title, Author, Publisher etc. are data members (attributes) of the class; display() and getPrice() are method members.
183

A special method used to initialize the data members of a class when an object is created is called a:

MCQ Easy
a. Destructor
b. Constructor
c. Accessor
d. Mutator
Information Technology (802) · Class 12 · Sep 26, 2026
Show answer
Constructor
Explanation: A constructor is a special method with the same name as the class, used to initialize data members when a new object is created.
184

Which of these statements about a Java constructor is TRUE?

MCQ Medium
a. It has a return type of void
b. It has the same name as the class and no return type
c. It must always take at least one parameter
d. It is called explicitly using the 'call' keyword
Information Technology (802) · Class 12 · Sep 26, 2026
Show answer
It has the same name as the class and no return type
Explanation: A constructor has the same name as the class, has NO return type, and may or may not take parameters; it is invoked automatically (not explicitly called) when a new object is created.
185

If a class defines only a parameterized constructor, what happens when you try to create an object without arguments, e.g., new Book()?

MCQ Hard
a. It works fine using default values
b. A compile error occurs (actual/formal arguments differ in length)
c. It creates a null object
d. It silently ignores the constructor
Information Technology (802) · Class 12 · Sep 26, 2026
Show answer
A compile error occurs (actual/formal arguments differ in length)
Explanation: Once a parameterized constructor is defined, calling new Book() without arguments causes a compile error, unless a no-argument (parameter-less) constructor is also explicitly defined.
186

A constructor that initializes all data members with default values and takes no parameters is called a:

MCQ Medium
a. Copy constructor
b. Parameterized constructor
c. Parameter-less (default) constructor
d. Static constructor
Information Technology (802) · Class 12 · Sep 26, 2026
Show answer
Parameter-less (default) constructor
Explanation: A parameter-less (default) constructor takes no parameters and typically initializes members with default values.
187

Which keyword is used to create a new object of a class in Java?

MCQ Easy
a. create
b. new
c. make
d. object
Information Technology (802) · Class 12 · Sep 26, 2026
Show answer
new
Explanation: The keyword 'new' is used to create a new object of a class, e.g., Book book1 = new Book();
188

Which operator is used to access data members and method members of an object?

MCQ Easy
a. Colon (:)
b. Dot (.)
c. Arrow (->)
d. Hash (#)
Information Technology (802) · Class 12 · Sep 26, 2026
Show answer
Dot (.)
Explanation: The dot (.) operator is used to access data/method members of an object, e.g., book1.display();
189

Why do all Java classes have access to methods like hashCode() and wait() even without defining them?

MCQ Hard
a. They are keywords, not methods
b. All Java classes inherit from the base class Object
c. They are automatically generated randomly
d. They must be imported manually
Information Technology (802) · Class 12 · Sep 26, 2026
Show answer
All Java classes inherit from the base class Object
Explanation: All Java classes inherit from the base class Object, which defines methods like hashCode() and wait() that become available to every class.
190

Which access modifier makes a data member or method visible ONLY within its own class?

MCQ Easy
a. public
b. private
c. static
d. final
Information Technology (802) · Class 12 · Sep 26, 2026
Show answer
private
Explanation: The private access modifier restricts visibility of a member to within its own class only.
191

By default (if no access modifier is specified), what is the visibility of a class member in Java?

MCQ Medium
a. private
b. public
c. protected
d. It has no visibility
Information Technology (802) · Class 12 · Sep 26, 2026
Show answer
public
Explanation: By default, all members of a class are public, meaning they are visible both within and outside the class.
192

A method that returns the value of a private data member is called a:

MCQ Easy
a. Setter method
b. Getter method
c. Constructor
d. Static method
Information Technology (802) · Class 12 · Sep 26, 2026
Show answer
Getter method
Explanation: A getter method returns the value of a data member, providing controlled read access to private data.
193

A method that allows controlled modification of a private data member's value (possibly with validation) is called a:

MCQ Easy
a. Getter method
b. Setter method
c. Constructor
d. Access modifier
Information Technology (802) · Class 12 · Sep 26, 2026
Show answer
Setter method
Explanation: A setter method sets/modifies a data member's value, and can include validation logic (e.g., rejecting a price below a minimum).
194

What is the main benefit of using getter and setter methods instead of allowing direct public access to data members?

MCQ Medium
a. It makes the code shorter
b. It allows validation/control over how the data member's value is changed
c. It makes the class run faster
d. It removes the need for a constructor
Information Technology (802) · Class 12 · Sep 26, 2026
Show answer
It allows validation/control over how the data member's value is changed
Explanation: Getter/setter methods allow controlled access — e.g., a setter can validate that a new price isn't below a minimum before allowing the change, which direct public access would not enforce.
195

Once a data member is declared private, it can still be accessed directly from outside the class using the dot operator.

TRUE FALSE Medium
a. TRUE
b. FALSE
Information Technology (802) · Class 12 · Sep 26, 2026
Show answer
FALSE
Explanation: Private members cannot be accessed directly from outside the class; they require public getter/setter methods for controlled access.
196

A constructor is called explicitly by the programmer using a special 'construct' statement.

TRUE FALSE Medium
a. TRUE
b. FALSE
Information Technology (802) · Class 12 · Sep 26, 2026
Show answer
FALSE
Explanation: A constructor is invoked automatically whenever a new object of the class is created (using 'new'); it is never called explicitly by name.
197

What is the difference between a Class and an Object? Give an example.

SHORT Easy
Information Technology (802) · Class 12 · Sep 26, 2026
Show answer
Explanation: A Class is a template/blueprint defining data members and method members (e.g., class Book with Title, Author, Price). An Object is an instance of that class holding actual data (e.g., book1 with Title='Game of Thrones', Author='George R Martin', Price=450).
198

Why is a constructor used in a class?

SHORT Easy
Information Technology (802) · Class 12 · Sep 26, 2026
Show answer
Explanation: A constructor is used to initialize the data members of a class (or perform other initialization) automatically whenever a new object of the class is created.
199

Design a Java class 'Book' with private data members Title, Author, Publisher, Genre (all String) and Price (double). Include a parameterized constructor, a getter and setter for Price (the setter should not allow price below 100), and a display() method.

LONG Hard
Information Technology (802) · Class 12 · Sep 26, 2026
Show answer
Explanation: class Book { private String Title, Author, Publisher, Genre; private double Price; Book(String t, String a, String p, String g, double pr) { Title=t; Author=a; Publisher=p; Genre=g; Price=pr; } double getPrice() { return Price; } void setPrice(double newprice) { if (newprice < 100) System.out.println("Price cannot be set lower than 100!"); else Price = newprice; } void display() { System.out.println(Title+" "+Author+" "+Publisher+" "+Genre+" "+Price); } }
200

Which keyword is used to bring a prebuilt Java class into a program for use?

MCQ Easy
a. include
b. import
c. using
d. require
Information Technology (802) · Class 12 · Sep 26, 2026
Show answer
import
Explanation: The keyword 'import' is used to bring a prebuilt class from a package into scope for use in a program.