Java Question Bank with Answers
Answers for Core Java Question Bank
Chapter 2: Objects and Classes.
Easy Level Questions (5 Marks Each)
1. What is a class in Java? How does it differ from an object?
Answer:
- Class: A class in Java is a blueprint or a template for creating objects .It is a user-defined data-type that defines the common properties (methods) that all objects of that type will have. A class is a logical construct and does not occupy memory when declared (Singh et al., 2021).
- Object: An object is a real-world entity and a concrete instance of a class. It is a physical entity that exists in memory and has its own unique state (values of its attributes) and behavior. Multiple objects can be created from a single class blueprint, each with its unique data.
Key Difference: A class is a logical definition or a type, whereas an object is a physical instance of that class. The class defines the structure, and objects are the actual data structures created according to that structure.
2. List the four access specifiers available in Java. Briefly state the visibility scope of public and private members.
Answer:
The four access specifiers (or access modifiers) available in Java are:
1. public
2. protected
3. default (no keyword, also known as package-private)
4. private
Visibility Scope:
- public members: These are accessible from anywhere: within the same class, within the same package, from subclasses (even in different packages), and from non-subclasses in different packages. It is the most permissive access level
- private members: These are accessible only from within the same class where they are declared. They are not visible to any other class, including subclasses in the same or different packages. It is the most restrictive access level, promoting encapsulation.
3. Explain the purpose of a constructor in Java. Can a class have more than one constructor?
Answer:
- Purpose of a Constructor: A constructor in Java is a special type of method that is invoked automatically when an object is created using the new keyword. Its primary purpose is to initialize the state (attributes) of an object at the time of its creation. This ensures that a newly created object is in a valid and usable state immediately after instantiation. Constructors typically assign arguments to fields or invoke the superclass constructor.
- Multiple Constructors: Yes, a class can have more than one constructor. This concept is known as constructor overloading. Constructor overloading allows a class to define multiple constructors with different parameter lists (different number, type, or order of parameters), providing flexibility in object initialization.
4. What is the this keyword used for in Java? Give a simple example.
Answer:
The this
keyword in Java is a reference variable that refers to the current object. It
is implicitly passed to every non-static method and constructor.
Primary Use:
1. Differentiating instance variables from local variables/parameters: When an instance variable and a local variable (or method parameter) have the same name, this is used to explicitly refer to the instance variable of the current object.
2. Invoking current class constructor (constructor chaining): this() can be used to call another constructor within the same class from within a constructor.
Example (for differentiating instance variables):
public class Product {
int id; // Instance variable
String name; // Instance variable
public Product(int id, String name) { // ‘id’ and ‘name’ here are local parameters
this.id = id; // ‘this.id’ refers to the instance variable
this.name = name; // ‘this.name’ refers to the instance variable
}
public void display() {
System.out.println("Product ID: " + this.id + ", Name: " + this.name);
}
}
5. Differentiate between a static variable and an instance variable in a Java class.
Answer:
|
Feature |
Static Variable |
Instance Variable |
|
Declaration |
Declared using the static keyword inside a class but outside any method. |
Declared inside a class but outside any method, without the static keyword. |
|
Memory |
Allocated memory only once, when the class is loaded into memory. It is stored in the method area. All objects of the class share the single copy of the static variable. |
Allocated memory each time an object (instance) of the class is created. Each object has its own separate copy of the instance variables. Stored in the heap area. |
|
Access |
Accessed using the class name (ClassName.staticVariable) or directly within static methods . |
Accessed only through an object’s reference (objectName.instanceVariable). Cannot be accessed directly using the class name. |
|
State |
Represents properties common to all objects of the class or values that should be shared |
Represents the unique state or properties of a particular object. |
|
Example |
public static int counter = 0; (e.g., to count objects created) |
public String name; (e.g., specific to each person object) |
6. Explain the function of the toString() method, inherited from the Object class. Why is it often overridden?
Answer:
- Function of toString(): The toString() method is a public method defined in the java.lang.Object class, which is the root class for all Java classes. Its primary function is to return a String representation of the object . By default, the Object class’s toString() method returns a string consisting of the class name, an "@" sign, and the unsigned hexadecimal representation of the object’s hash code.
- Why it’s Often Overridden: The default implementation of toString() is usually not very helpful or meaningful to humans because it provides memory address information rather than meaningful data. Developers often override toString() in their custom classes to:
1. Provide a meaningful description: Return a string that represents the object’s current state (values of its important attributes) in a human-readable format.
2. Aid in debugging and logging: When an object is printed directly using System.out.println(object) or used in string concatenation, Java implicitly calls its toString() method. A well-overridden toString() makes debugging logs and console outputs much more informative.
3. Improve readability: Simplifies showing an object’s contents without manually accessing each field.
7. What are Wrapper Classes in Java? Give two examples and their corresponding primitive types.
Answer:
Wrapper Classes in Java are classes that provide a way to use primitive
data types (like int, char, boolean, double) as
objects. They "wrap" the primitive value inside an object, allowing
primitives to be treated as objects. Each primitive type has a corresponding
wrapper class in the java.lang package.
Necessity: Primitive types are not objects (“Proceedings of the 19th Conference on Computer Science and Intelligence Systems, which means they cannot be directly used in situations where objects are required, such as:
- Storing values in Java Collections Framework (e.g., ArrayList, HashMap), which can only store objects.
- Working with Java Generics, which also require object types.
- Passing arguments to methods that expect objects.
- Supporting null values (primitives cannot be null).
Two Examples and their Corresponding Primitive Types:
1. Wrapper Class: Integer
o Corresponding Primitive Type: int
o Example Usage: Integer myInt = 10; (autoboxing)
2. Wrapper Class: Double
o Corresponding Primitive Type: double
o Example Usage: Double myDouble = 3.14;