Question 1
………….. is the basic unit in Object Oriented Programming.
(A) Object
(B) Class
(C) (A) and (B) both
(D) Method
Answer: (C) (A) and (B) both
Explanation: In Object-Oriented Programming (OOP), both objects and classes are fundamental concepts. A class is like a blueprint that defines properties and behaviors, while an object is an instance of a class, created based on the class blueprint.
Memory Tip: Remember “Class builds, Object is built.” They go hand in hand, so both are basic units in OOP.
Question 2
What is there in a class?
(A) Attribute
(B) Method
(C) (A) and (B) both
(D) (A) or (B)
Answer: (C) (A) and (B) both
Explanation: A class typically contains both attributes (data or variables) and methods (functions or behaviors). Attributes represent the properties, while methods define what the class can do.
Memory Tip: Think of a class as a “storage box” that holds both data (attributes) and actions (methods).
Question 3
What is a class?
(A) A small room
(B) Object name with different properties
(C) Object structure with common properties
(D) None of these
Answer: (C) Object structure with common properties
Explanation: A class provides a structure with common properties that can be used to create objects with similar features. It defines attributes and methods that objects created from the class will have.
Memory Tip: Remember “Class contains common.” It’s a structure that provides shared properties.
Question 4
What is the format to define a class?
(A) class <class Name> { <variables> <methods> }
(B) class <classname> { <variables> (methods) }
(C) class <class Name> { <methods> <variables> }
(D) class <classname> { <variables> <methods> }
Answer: (B) class <classname> { <variables> (methods) }
Explanation: The correct syntax for defining a class in Java is class <classname> { <variables> (methods) }
. This structure clearly separates variables and methods within a class.
Memory Tip: “Class, Variables, Methods” – Keep them in that order for defining a class.
Question 5
Attribute is defined by ……………. in a class.
(A) variable
(B) method
(C) behavior
(D) none of these
Answer: (A) variable
Explanation: An attribute in a class is represented by a variable. Variables hold the data or properties of a class.
Memory Tip: “Attributes are Variables” – Remember that attributes store data, and variables are data holders.
Question 6
What is the use of a method in a class?
(A) To get the attributes
(B) To modify the attributes
(C) (A) and (B) both
(D) For calculation
Answer: (C) (A) and (B) both
Explanation: Methods are used to interact with attributes. They can retrieve attribute values and modify them as needed.
Memory Tip: “Methods manage attributes” – They help both to get and change the attribute values.
Question 7
Class is made of how many elements?
(A) 2
(B) 3
(C) 4
(D) 5
Answer: (A) 2
Explanation: A class is composed of two main elements: attributes (variables) and behaviors (methods).
Memory Tip: “Class = 2 Elements: Attributes + Behaviors.”
Question 8
……………. element is included in a class in Java.
(A) Attribute
(B) Behavior
(C) (A) or (B)
(D) Method
Answer: (C) (A) or (B)
Explanation: A class in Java includes either attributes or behaviors (or both). Attributes represent data, while behaviors are the actions performed by the class.
Memory Tip: “Class can have either Attributes, Behaviors, or both.”
Question 9
Behavior is defined by ……………. in a class.
(A) method
(B) program
(C) (A) and (B) both
(D) none of these
Answer: (A) method
Explanation: The behavior of a class is defined by its methods, which are functions that determine what actions the class can perform.
Memory Tip: “Methods = Behavior” – Methods control what the class can do.
Question 10
……………….. is the syntax to declare an object creation from a class.
(A) <class name> <variable name>
(B) (class name> cvariables name>
(C) <variables name) <classname>
(D) None of these
Answer: (A) <class name> <variable name>
Explanation: The correct syntax to create an object from a class in Java is to specify <class name> <variable name>
. This declares a new variable as an instance of the specified class.
Memory Tip: “Class + Variable = Object” – Use this format to create objects.
Question 11
………… keyword allots the memory to the object while creating the object.
(A) obj
(B) new
(C) NEW
(D) (A) and (B) both
Answer: (B) new
Explanation: The new keyword is used in Java to allocate memory for a new object. When we create an object using new
, Java allocates memory for it on the heap.
Memory Tip: Remember “new for new memory” – the new keyword is essential to allocate memory for objects.
Question 12
……………. is called to assign a value to a newly created object.
(A) Conductor
(B) Constructor
(C) (A) or (B)
(D) None of these
Answer: (B) Constructor
Explanation: A constructor in Java is a special method used to initialize objects. It is automatically called when an object is created.
Memory Tip: “Constructor constructs (initializes)” – Think of a constructor as setting up (constructing) the initial values.
Question 13
In Java, a class is of …………………. type same as a data type.
(A) int and boolean
(B) int and float
(C) boolean and float
(D) none of these
Answer: (A) int and boolean
Explanation: In Java, classes can be used as types, similar to data types like int
and boolean
. This means that objects created from classes can be assigned to variables, parameters, and return types, just like any primitive type.
Memory Tip: “Class is like a type” – Classes act as types, just like int
and boolean
.
Question 14
What is the use of the name of a class in Java?
(A) To declare the type of a variable
(B) To define a type of formal parameter
(C) To define the type of return values
(D) All of these
Answer: (D) All of these
Explanation: A class name in Java can be used to declare variable types, define parameters, and set return types, making it versatile in defining data structures.
Memory Tip: “Class name = Type of variable, parameter, or return” – It helps in multiple ways.
Question 15
Which statement is not true?
(A) Any variable never stores the object in Java.
(B) The variable declared with class type only indicates the object
(C) The variable of class type is called a reference variable
(D) Any variable value is stored in an object first
Answer: (D) Any variable value is stored in an object first
Explanation: In Java, objects are stored in memory, and variables of class types act as references (or pointers) to these objects. A variable does not directly hold the object itself; it references its location in memory.
Memory Tip: “Class type = Reference, not storage” – Reference variables point to objects but do not directly store them.
Question 16
Using ……………… keyword an object is created.
(A) create
(B) new
(C) NEW
(D) CREATE
Answer: (B) new
Explanation: The new keyword is used to create an object in Java. It allocates memory and returns a reference to the object.
Memory Tip: Think “new = new object” – this keyword initiates object creation.
Question 17
What is heap?
(A) Special word of Java
(B) Special part of memory
(C) Special subsidiary part of memory
(D) None of these
Answer: (B) Special part of memory
Explanation: The heap is a special part of memory used for dynamic allocation, where objects are stored.
Memory Tip: “Heap holds objects” – Remember that the heap is a special memory area for objects.
Question 18
What is kept in the heap?
(A) Object
(B) class Address
(C) classes
(D) All of these
Answer: (A) Object
Explanation: Objects are stored in the heap memory, whereas other information like method definitions and static variables are stored elsewhere.
Memory Tip: “Heap is for objects” – Only objects go in the heap.
Question 19
What is the use of constructor while creating an object?
(A) To allot memory
(B) To initialize the newly created object
(C) (A) and (B) both
(D) None of these
Answer: (C) (A) and (B) both
Explanation: A constructor is used to initialize a new object, and indirectly helps in allocating memory as part of the object creation process.
Memory Tip: Think “Constructor = Memory + Setup” – Constructors both set up the object and are called during memory allocation.
Question 20
To declare an object of class ‘Room’ and make the reference variable r1, the correct statement is:
(A) r1 = Room ();
(B) r1 = new Room ();
(C) Room () = r1;
(D) r1 – new room ();
Answer: (B) r1 = new Room ();
Explanation: The syntax r1 = new Room ();
correctly declares an object of class Room
and assigns its reference to r1
. The new keyword allocates memory and calls the Room
constructor.
Memory Tip: “new Room() = new Room object” – The correct way to create a new object is with new ClassName()
.
Question 21
If we don’t use the argument in the declared object, then what is called by the empty parentheses?
(A) Predefined constructor
(B) Zero value
(C) This is not possible
(D) -1 value
Answer: (A) Predefined constructor
Explanation: When an object is created without any arguments, a default constructor (often called a predefined or no-argument constructor) is called, initializing the object with default values.
Question 22
In the statement “r1 = new Room ();”, what does r1 hold?
(A) Object is stored in variable r1
(B) Variable r1 holds only the address of object
(C) Holds the value of Room()
(D) All of these
Answer: (B) Variable r1 holds only the address of object
Explanation: In Java, r1
is a reference variable that holds the memory address of the Room
object, not the object itself.
Question 23
Data is actually stored where?
(A) Within an object
(B) Within a class
(C) (A) and (B) both
(D) (A) or (B)
Answer: (A) Within an object
Explanation: Data (attributes or instance variables) are stored within an object. Classes provide the blueprint, while objects hold the actual data.
Question 24
What is the use of garbage collector?
(A) Looks for unused objects
(B) Reclaims the memory that those objects are using
(C) (A) and (B) both
(D) To monitor the program properly
Answer: (C) (A) and (B) both
Explanation: The garbage collector in Java automatically identifies objects that are no longer in use and reclaims their memory, helping to manage memory efficiently.
Question 25
In OOP, creating an object is called …………… .
(A) object installation
(B) object Instantiation
(C) object Creation
(D) object Preparation
Answer: (B) object Instantiation
Explanation: The term instantiation is used to describe the process of creating an instance (object) of a class in OOP.
Question 26
What is the other word used for object?
(A) Instance of a class
(B) Data of class
(C) Variable
(D) Parameter
Answer: (A) Instance of a class
Explanation: An object is also known as an instance of a class, as it represents a concrete occurrence of the class blueprint.
Question 27
……………… is the abstract representation of an object.
(A) Instance
(B) Class
(C) Parameter
(D) None of these
Answer: (B) Class
Explanation: A class is the abstract blueprint or template for objects, defining the structure and behavior that the objects will have.
Question 28
……………… is the concrete representation of an object.
(A) Instance
(B) Class
(C) Variable
(D) Parameter
Answer: (A) Instance
Explanation: An instance is the actual, concrete representation of an object created based on a class.
Question 29
In OOP, …………….. words are used interchangeably.
(A) Instance
(B) Object
(C) (A) and (B) both
(D) None of these
Answer: (C) (A) and (B) both
Explanation: The terms instance and object are often used interchangeably in OOP, as an instance is essentially an object of a class.
Question 30
Where is a method created in Java?
(A) In object
(B) In class
(C) In program
(D) Not possible
Answer: (B) In class
Explanation: Methods are defined within a class and describe the behavior that instances (objects) of the class can perform.
Question 31
Question: What is the use of an instance method?
Options:
(A) To get the value of an instance variable
(B) To change the value of an instance variable
(C) To define the behavior of an object
(D) All of these
Answer: (D) All of these
Explanation: An instance method in Java can retrieve or change instance variables and define the behavior of an object. All three options (A), (B), and (C) are true for instance methods.
Memory Tip: Think of an instance method as a tool that does it all for the object: it can get data, change data, and define behavior.
Question 32
Question: Which of the following is not an instance method?
Options:
(A) setAttr()
(B) display()
(C) width()
(D) area()
Answer: (C) width()
Explanation: Instance methods typically perform actions or behaviors. setAttr and display sound like actions (methods), while width is more descriptive, representing a characteristic of an object rather than an action.
Memory Tip: Methods are actions. Words like set and display indicate actions, while width sounds more like a property of an object.
Question 33
Question: Which of the following is not an instance variable?
Options:
(A) length
(B) nWindows
(C) width
(D) setAttr()
Answer: (D) setAttr()
Explanation: Instance variables represent properties or characteristics of an object. setAttr is a method (action) rather than a property, so it’s not an instance variable.
Memory Tip: Variables are usually nouns like length and width, describing attributes. Methods or actions are more like verbs.
Question 34
Question: Which operator is used to access instance variables?
Options:
(A) .
(B) ,
(C) #
(D) ;
Answer: (A) . (dot)
Explanation: The dot operator (.
) is used in Java to access the members (variables and methods) of an object. It allows you to “reach into” an object and use its contents.
Memory Tip: Think of the dot (.
) as a connector to “reach into” the object.
Question 35
Question: Which is the syntax to access an instance variable?
Options:
(A) <object reference> ; <instance variable>
(B) <object reference> . <instance variable>
(C) <instance variable> . <object reference>
(D) <object reference> , <instance variable>
Answer: (B) <object reference> . <instance variable>
Explanation: In Java, we use <object reference> . <instance variable>
to access an instance variable. The dot operator connects the object reference to its instance variable.
Memory Tip: Object reference comes first, followed by the dot and then the variable. Just think of connecting them in order like a path.
Question 36
Question: What is the associativity of the dot operator?
Options:
(A) Left to right
(B) Right to left
(C) Defining a value
(D) None of these
Answer: (A) Left to right
Explanation: The dot operator in Java is left-associative, which means expressions are evaluated from left to right.
Memory Tip: Imagine reading a sentence in left-to-right order—the dot acts the same way in expressions.
Question 37
Question: When is the dot not needed for an instance variable?
Options:
(A) When we use the instance variable inside different methods of a class
(B) When we use the instance variable in a method of the same class
(C) In a simple Java program
(D) This is not possible
Answer: (B) When we use the instance variable in a method of the same class
Explanation: Inside a method of the same class, you can directly access instance variables without using the dot, as they are directly accessible.
Memory Tip: Inside the same class, the method “already knows” the variable—no dot is needed.
Question 38
Question: Which variable can be accessed without creating an instance of a class?
Options:
(A) Static
(B) Object
(C) (A) and (B) both
(D) This is not possible
Answer: (A) Static
Explanation: Static variables belong to the class itself rather than any specific instance. Therefore, they can be accessed without creating an object.
Memory Tip: Static means belongs to class—no need for an instance.
Question 39
Question: Which variable does not begin with predefined values?
Options:
(A) Local
(B) Instance
(C) Class
(D) All of these
Answer: (A) Local
Explanation: Local variables, defined within a method, do not get default values and must be initialized explicitly before use.
Memory Tip: Local variables are defined locally in a method and need to be given a value—Java does not assign a default.
Question 40
Question: Which variables have predefined values?
Options:
(A) Local
(B) Instance
(C) Class
(D) (B) and (C) both
Answer: (D) (B) and (C) both
Explanation: Both instance and class (static) variables are assigned default values if not initialized, unlike local variables.
Memory Tip: Instance and class variables are connected to the class structure, so they are automatically assigned defaults.
Question 41
Question: Which variables are defined within methods or blocks?
Options:
(A) Instance
(B) Local
(C) Class
(D) (B) and (C) both
Answer: (B) Local
Explanation: Local variables are declared within methods or blocks, and their scope is limited to those blocks.
Memory Tip: Local variables are local to a method or block, meaning they only exist inside it.
Question 42
Question: Which variables are defined in a particular class but outside any method?
Options:
(A) Local
(B) Instance
(C) (A) and (B) both
(D) Class
Answer: (B) Instance
Explanation: Instance variables are declared in a class but outside any methods. They belong to instances (objects) of the class.
Memory Tip: Instance variables live inside the class but outside methods, representing properties of an object.
Question 43
Question: Which variables are allotted memory from the heap?
Options:
(A) Local
(B) Class variable
(C) Instance
(D) (B) and (C) both
Answer: (C) Instance
Explanation: Instance variables are allocated memory on the heap because they are part of an object’s memory space, which is stored in the heap.
Memory Tip: Remember heap as the home for instance variables since they belong to objects in memory.
Question 44
Question: Which variable is defined with the static
keyword in a class but outside any method?
Options:
(A) Local
(B) Class
(C) Instance
(D) (B) and (C) both
Answer: (B) Class
Explanation: Class variables (or static variables) are declared with the static
keyword, which means they belong to the class itself and are shared among all objects.
Memory Tip: Class variables are shared by all instances, so they are static and outside methods in the class.
Question 45
Question: Which variable is assigned a value only once but can be used by all objects?
Options:
(A) Local
(B) Class
(C) Instance
(D) Special
Answer: (B) Class
Explanation: Class variables are shared by all objects of the class and are typically assigned once. They retain their value across all instances of the class.
Memory Tip: Think of class variables as common to the class—they are assigned once and shared by all objects.
Question 46
Question: Which is the third step to create an object?
Options:
(A) Declaration
(B) Instantiation
(C) Initialization
(D) Comment
Answer: (C) Initialization
Explanation: The three steps to create an object in Java are declaration, instantiation, and initialization. Initialization is the final step where the object is set up with specific values.
Memory Tip: Remember the DII acronym for the three steps—Declaration, Instantiation, Initialization.
Question 47
Question: What is the meaning of polymorphism?
Options:
(A) Many forms
(B) More than one form
(C) (A) and (B) both
(D) Infinite form
Answer: (C) (A) and (B) both
Explanation: Polymorphism in programming allows a single entity (like a method) to take on multiple forms. This concept is central to object-oriented programming.
Memory Tip: Think of poly as many and morph as forms—many forms.
Question 48
Question: Different methods with the same name refer to …………….. .
Options:
(A) Polymorphism
(B) Constructors
(C) Accessor method
(D) Inheritance
Answer: (A) Polymorphism
Explanation: Polymorphism allows methods with the same name to perform different tasks, depending on the context (overloading or overriding).
Memory Tip: Poly means many, so polymorphism allows many methods with the same name to take on different behaviors.
Question 49
Question: Different methods with the same name but different signatures refer to ………….. .
Options:
(A) Method overloading
(B) Method writeloading
(C) (A) or (B)
(D) Constructors
Answer: (A) Method overloading
Explanation: Method overloading allows multiple methods with the same name but different parameter lists (signatures) within a class.
Memory Tip: Overloading a method means giving it extra “loads”—different parameters.
Question 50
Question: Which is the special method that is invoked when creating an object?
Options:
(A) Inheritance
(B) Constructors
(C) Public
(D) Mutator
Answer: (B) Constructors
Explanation: A constructor is a special method called automatically when an object is created. It initializes the object.
Memory Tip: Think of constructors as building blocks that construct or initialize the object when it’s created.
Question 51
Question: What is mainly used to initialize the values?
Options:
(A) Constructors
(B) Inheritance
(C) Composition
(D) All of these
Answer: (A) Constructors
Explanation: Constructors are primarily used to initialize objects by setting up initial values when an object is created.
Memory Tip: Construct means to build or set up something, which helps remember that constructors set up initial values for objects.
Question 52
Question: What is called a constructor without variables?
Options:
(A) Inheritance
(B) Default Constructors
(C) Mutator method
(D) All of these
Answer: (B) Default Constructors
Explanation: A default constructor is a constructor with no parameters. If no constructors are defined, Java automatically provides a default constructor.
Memory Tip: Default means basic or standard—a default constructor is a basic constructor without parameters.
Question 53
Question: Which statement is incorrect about constructors?
Options:
(A) Constructor name and class name are similar.
(B) Constructors do not have a return type.
(C) Constructors can be invoked from anywhere in the program.
(D) When an object is created using a new
operator, then only it can be invoked by a constructor.
Answer: (C) Constructors can be invoked from anywhere in the program
Explanation: Constructors are invoked only when an object is created, not from anywhere in the program.
Memory Tip: Constructors are only called when creating an object, not at any other point.
Question 54
Question: If we try to create an object with a constructor and without variables, what will be the output?
Options:
(A) 0
(B) 1
(C) Compiler shows an error
(D) -1
Answer: (C) Compiler shows an error
Explanation: If the program tries to use a constructor without a matching signature in the class, the compiler will show an error.
Memory Tip: Think of the compiler as strict—if you try to use an undefined constructor, it shows an error.
Question 55
Question: What is added to a program when a user-defined constructor is created without a variable?
Options:
(A) New class is created
(B) <classname>() { };
(C) <classname>() { }
(D) Constructor without variable is not possible
Answer: (C) <classname>() { }
Explanation: When a constructor is created without parameters, it is written in the format <classname>() { }
.
Memory Tip: Remember that a simple constructor format is just the class name with empty parentheses and braces: <classname>() { }
.
Question 56
Question: Why is an access modifier called a “visibility modifier”?
Options:
(A) Access control is the visibility control
(B) Access control can do modification
(C) Access control is not the visibility control
(D) None of these
Answer: (A) Access control is the visibility control
Explanation: Access modifiers control the visibility of classes, methods, and variables. This is why they’re called visibility modifiers.
Memory Tip: Think of visibility as how “visible” or accessible a part of the code is.
Question 57
Question: How many visibility levels are there to make a method or variable secure?
Options:
(A) 2
(B) 3
(C) 4
(D) 5
Answer: (C) 4
Explanation: In Java, there are four visibility levels: public
, protected
, default
(package-private), and private
.
Memory Tip: Think of the “Four P’s” (see next question) for the four levels: Public, Protected, Package-private (default), Private.
Question 58
Question: What is the visibility level to make a method or variable secure?
Options:
(A) Four P’s
(B) Four Q’s
(C) Four R’s
(D) This is not possible
Answer: (A) Four P’s
Explanation: The four visibility levels in Java are often remembered as the “Four P’s”: Public, Protected, Package-private (default), and Private.
Memory Tip: Remember Four P’s for visibility levels.
Question 59
Question: Which is not a visibility level of security?
Options:
(A) Public
(B) Protect
(C) Initial
(D) Private
Answer: (C) Initial
Explanation: In Java, Initial
is not a valid visibility level; the correct levels are public
, protected
, private
, and default (package-private).
Memory Tip: Remember that Initial isn’t related to visibility—think PPP (Public, Protected, Private).
Question 60
Question: Where is the visibility level declared in a program?
Options:
(A) Before the type of method or a variable
(B) After the type of method or a variable
(C) (A) or (B)
(D) None of these
Answer: (A) Before the type of method or a variable
Explanation: In Java, the visibility level (access modifier) is declared before the type of method or variable.
Memory Tip: Access modifiers “open” the statement, so they go before the type of method or variable.
Question 61
Question: Which visibility is used to give an appropriate form to different classes?
Options:
(A) Public
(B) Package
(C) Protected
(D) Private
Answer: (B) Package
Explanation: Package-level visibility (also known as default visibility) allows classes within the same package to access each other’s members without using additional modifiers.
Memory Tip: Package visibility is the default level, allowing classes in the same package to communicate.
Question 62
Question: When is a class kept in the default package?
Options:
(A) When there is no package statement in a file
(B) When there is a package statement in a file
(C) Anytime it can be kept
(D) This is not possible
Answer: (A) When there is no package statement in a file
Explanation: If no package
statement is provided, the class is placed in the default package.
Memory Tip: No package statement means default package.
Question 63
Question: What is the syntax of a package statement?
Options:
(A) {Packagename};
(B) <packageName>;
(C) <packagename>
(D) (PackageName);
Answer: (B) <packageName>;
Explanation: In Java, a package statement starts with package
followed by the package name in lowercase. Example: package myPackage;
Memory Tip: Syntax starts with lowercase “package”, as in package myPackage;
Question 64
Question: If a method or variable in the class is to be accessed by any outside class, which access modifier should be used?
Options:
(A) public
(B) package
(C) protection
(D) private
Answer: (A) public
Explanation: The public
modifier makes a method or variable accessible from any other class.
Memory Tip: Public means open to all, accessible by any class.
Question 65
Question: Which is the most accessible access modifier?
Options:
(A) Package
(B) Protection
(C) Public
(D) Private
Answer: (C) Public
Explanation: Public
allows maximum accessibility, enabling access to a class or member from anywhere.
Memory Tip: Public means maximum accessibility.
Question 66
Question: Which access modifier provides visibility to a class defined in another package?
Options:
(A) Package
(B) Public
(C) Protection
(D) Private
Answer: (B) Public
Explanation: The public
modifier makes a class accessible even from different packages.
Memory Tip: Public means open and accessible across packages.
Question 67
Question: If public
access is to be provided to everyone, which method keyword uses public
?
Options:
(A) main()
(B) start()
(C) open()
(D) none of these
Answer: (A) main()
Explanation: In Java, the main
method is declared public
so it can be accessed from outside the class when the program starts.
Memory Tip: Remember, public static void main—main() must be public to start a program.
Question 68
Question: Which is the second-level access modifier?
Options:
(A) Public
(B) Package
(C) Protection
(D) Private
Answer: (B) Package
Explanation: Package (or default) access is a level below public
but more accessible than protected
and private
.
Memory Tip: Package level is like internal access—open within the package but not outside.
Question 69
Question: Which access modifier does not have a specific name?
Options:
(A) Public
(B) Protection
(C) Package
(D) Private
Answer: (C) Package
Explanation: The package-level modifier has no specific keyword and is known as “default” or “package-private” visibility.
Memory Tip: Package level is default—no specific keyword is used.
Question 70
Question: Which access modifier has a smaller range than a public variable?
Options:
(A) package
(B) private
(C) (A) and (B) both
(D) protection
Answer: (A) package
Explanation: Package-level access is less open than public
since it only allows access within the same package.
Memory Tip: Package visibility is within the package only, whereas public is open to all.
Question 71
Question: In which access modifier can a variable or method be accessed from anywhere?
Options:
(A) package
(B) private
(C) public
(D) protection
Answer: (C) public
Explanation: The public
access modifier allows a variable or method to be accessed from anywhere, whether within the same package or from other packages.
Memory Tip: Public means accessible by everyone, everywhere.
Question 72
Question: Which access modifier is used to access only from the subclass or for joint use of a method declared as ‘friend’?
Options:
(A) package
(B) private
(C) public
(D) protect
Answer: (D) protect
Explanation: The protected
access modifier allows access from within the same package or from subclasses, even if they are in different packages.
Memory Tip: Protected is for subclasses and the package.
Question 73
Question: Visibility of the protected access modifier is less than which of the following?
Options:
(A) public
(B) package
(C) (A) and (B) both
(D) private
Answer: (C) (A) and (B) both
Explanation: protected
visibility is less than public
and package
because protected
restricts access to subclasses and classes within the same package, while public
allows access everywhere and package
is accessible within the package.
Memory Tip: Protected is more restrictive than public and package.
Question 74
Question: The visibility of the protected access modifier is more than which of the following?
Options:
(A) public
(B) package
(C) (A) and (B) both
(D) private
Answer: (D) private
Explanation: protected
allows access within the same package and through inheritance, whereas private
restricts access to within the same class only.
Memory Tip: Protected is more accessible than private but less than public/package.
Question 75
Question: Which access modifier provides the highest level of protection?
Options:
(A) Public
(B) Private
(C) Package
(D) All of these
Answer: (B) Private
Explanation: The private
access modifier provides the highest level of protection as it restricts access to the variable or method within the same class only.
Memory Tip: Private means maximum protection—access is limited to the same class only.
Question 76
Question: Which access modifier has the least visibility?
Options:
(A) Public
(B) Package
(C) Protect
(D) Private
Answer: (B) Package
Explanation: The package
access modifier (default) has the least visibility because it allows access only within the same package and not from outside packages.
Memory Tip: Package is more restrictive than public
but less restrictive than private
.
Question 77
Question: Which access modifier provides a general level of protection?
Options:
(A) Private
(B) Protect
(C) Package
(D) All of these
Answer: (B) Protect
Explanation: The protected
access modifier offers a balanced level of protection, allowing access within the same package and subclasses, which is a general level of accessibility.
Memory Tip: Protected is middle ground protection.
Question 78
Question: Which access modifier has the feature of data encapsulation?
Options:
(A) package
(B) public
(C) private
(D) protect
Answer: (C) private
Explanation: The private
access modifier supports data encapsulation because it restricts access to the data, ensuring that it can only be accessed through methods within the class.
Memory Tip: Private is essential for data encapsulation.
Question 79
Question: What is the best way to provide data encapsulation?
Options:
(A) To make the data private as less as possible
(B) To make the data private as more as possible
(C) To make the data protected as more as possible
(D) To make the data protected as less as possible
Answer: (B) To make the data private as more as possible
Explanation: The best approach for data encapsulation is to make the data as private
as possible, ensuring that access is controlled via public methods or constructors.
Memory Tip: Private data means better control and encapsulation.
Question 80
Question: If no modifier is declared, which modifier is taken as the default?
Options:
(A) package
(B) private
(C) public
(D) protected
Answer: (A) package
Explanation: If no access modifier is declared, the default access modifier is package
, meaning the variable or method is accessible only within the same package.
Memory Tip: No modifier means package by default.
Question 81
Question: When we declare the data as private, what is our purpose?
Options:
(A) To access the method of another class directly
(B) To make the method of another class protected by modifying it
(C) (A) or (B)
(D) To save time
Answer: (C) (A) or (B)
Explanation: Declaring data as private
is meant to encapsulate and protect the data, restricting direct access from outside the class. We can provide controlled access using accessor and mutator methods.
Memory Tip: Private data ensures controlled access.
Question 82
Question: When is an accessor method possible?
Options:
(A) When we permit others to use the data
(B) When we modify the data and permit others
(C) (A) or (B)
(D) (A) and (B) both
Answer: (A) When we permit others to use the data
Explanation: An accessor method (getter) is used to allow other classes to access (but not modify) the data in a controlled way.
Memory Tip: Accessor methods allow data reading.
Question 83
Question: When is a mutator method possible?
Options:
(A) When we permit others to use the data
(B) When we modify the data and permit others
(C) (A) or (B)
(D) (A) and (B) both
Answer: (B) When we modify the data and permit others
Explanation: A mutator method (setter) is used to modify the value of a private data member and permit other classes to change its value.
Memory Tip: Mutator methods allow data modification.
Question 84
Question: Accessor method is known as __.
Options:
(A) acceter
(B) setter
(C) getter
(D) mutator
Answer: (C) getter
Explanation: The accessor method is also called a getter, as it is used to retrieve the value of a private variable.
Memory Tip: Get and Retrieve both start with G — making it easy to remember as getter.
Question 85
Question: Mutator method is known as __.
Options:
(A) acceter
(B) setter
(C) getter
(D) mutator
Answer: (B) setter
Explanation: The mutator method is also called a setter, as it is used to modify the value of a private variable.
Memory Tip: Set and Change both start with S — making it easy to remember as setter.
Question 86
Question: What will be the accessor method for length
?
Options:
(A) setLength
(B) length
(C) getLength()
(D) getlength()
Answer: (C) getLength()
Explanation: The accessor (getter) method for a variable length
is typically named getLength()
in Java.
Memory Tip: Get the length with getLength()
.
Question 87
Question: __ method should be used if we want to permit other methods to only read the data values.
Options:
(A) setter
(B) getter
(C) reader
(D) None of these
Answer: (B) getter
Explanation: A getter method allows other classes to read (but not modify) the value of a variable.
Memory Tip: Getter is for reading data.
Question 88
Question: What will be the mutator method for length
?
Options:
(A) setlength()
(B) setLength()
(C) getLength()
(D) getlength
Answer: (B) setLength()
Explanation: The mutator (setter) method for a variable length
is typically named setLength()
in Java.
Memory Tip: Set the length with setLength()
.
Question 89
Question: _ will define the value of the length
attribute in the setLength()
method.
Options:
(A) void setLength() {length = /;}
(B) void setLength() {length = /:;}
(C) void setLength(float 1) {length = /;}
(D) void setLength(float 1) {length = /;}
Answer: (C) void setLength(float 1) {length = /;}
Explanation: The correct method syntax for defining the value of the length
attribute is: void setLength(float length) { this.length = length; }
.
Memory Tip: The set method receives a parameter to assign a value to the attribute.
Question 90
Question: __ is the use of inheritance.
Options:
(A) To stop the existing class and create a new class
(B) To build a new class with added capabilities by expanding the existing class
(C) To use the characteristics of the existing class again
(D) (B) and (C) both
Answer: (D) (B) and (C) both
Explanation: Inheritance allows you to build a new class by expanding the capabilities of an existing class, and it also allows for reuse of the characteristics of the parent class.
Memory Tip: Inheritance allows extension and reuse of existing classes.
Question 91
Question: Inheritance is the model of __ type relationship.
Options:
(A) is-a
(B) has-a
(C) have-a
(D) none of these
Answer: (A) is-a
Explanation: Inheritance models an “is-a” relationship, where the subclass is a specialized version of the superclass.
Memory Tip: Think of inheritance as “A dog is a mammal” — it’s an is-a relationship.
Question 92
Question: All common features are kept in __ class.
Options:
(A) super class
(B) sub class
(C) main class
(D) it can be kept anywhere
Answer: (A) super class
Explanation: The superclass contains all the common features (variables and methods) that are inherited by subclasses.
Memory Tip: The superclass is the parent where shared characteristics live.
Question 93
Question: __ inherits all instance variables and methods from the superclass and may have its own added variables and methods.
Options:
(A) Sub class
(B) Super class
(C) Main class
(D) Subsidiary
Answer: (A) Sub class
Explanation: The subclass inherits the properties of the superclass but can also add its own specific properties.
Memory Tip: The subclass is like a child that inherits from the parent and can have extra traits.
Question 94
Question: __ keyword is used to make a subclass in Java.
Options:
(A) create
(B) extends
(C) prepare
(D) None of these
Answer: (B) extends
Explanation: The extends
keyword is used to create a subclass in Java, indicating inheritance from a superclass.
Memory Tip: “extends” — the subclass extends the features of the superclass.
Question 95
Question: To call the constructor of the superclass, __ keyword is used in the constructor of the subclass.
Options:
(A) extends
(B) call
(C) super
(D) subs
Answer: (C) super
Explanation: The super
keyword is used to call the constructor of the superclass from the subclass.
Memory Tip: “super” is for calling the superclass constructor.
Question 96
Question: __ method is used to display the attributes of an object.
Options:
(A) display()
(B) show()
(C) print()
(D) None of these
Answer: (B) show()
Explanation: The method show()
is commonly used to display the attributes of an object, but other methods like display()
or print()
can also be used depending on the implementation.
Memory Tip: Think of show as showing the object’s details.
Question 97
Question: If there is a method with a common signature in the superclass and subclass, then __ method is ignored in the subclass.
Options:
(A) display()
(B) superclass
(C) subclass
(D) inheritance
Answer: (B) superclass
Explanation: If the subclass has a method with the same signature as one in the superclass, the method in the superclass is overridden by the subclass method.
Memory Tip: The subclass version of the method is the one that gets used (overrides the superclass).
Question 98
Question: To invoke the display()
method of the superclass, __ to be used in the show()
method.
Options:
(A) show.display()
(B) super.display()
(C) super:display()
(D) extend.display()
Answer: (B) super.display()
Explanation: The super.display()
method calls the display()
method from the superclass.
Memory Tip: Use super
to access the superclass method.
Question 99
Question: __ type of relationship between two classes is created in composition and aggregation.
Options:
(A) has-a
(B) have-a
(C) is-a
(D) None of these
Answer: (A) has-a
Explanation: Both composition and aggregation model a has-a relationship, where one class contains or is composed of another class.
Memory Tip: Composition and aggregation are “has-a” relationships (like a car has an engine).
Question 100
Question: __ class creates a has-a type relationship.
Options:
(A) Composition
(B) Aggregation
(C) (A) and (B) both
(D) Inheritance
Answer: (C) (A) and (B) both
Explanation: Both composition and aggregation create a has-a relationship. Composition is a stronger form of association, whereas aggregation is a weaker one.
Memory Tip: Both composition and aggregation involve a has-a relationship.