Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

51.

Which of the following is a garbage collection technique?(a) Cleanup model(b) Mark and sweep model(c) Space management model(d) Sweep modelI got this question in an international level competition.Origin of the question is Heap and Garbage Collection in division Classes and Methods of Java

Answer»

Correct answer is (b) Mark and SWEEP model

For explanation I would say: A mark and sweep garbage collection consists of TWO phases, the mark phase and the sweep phase. I mark phase all the objects reachable by java threads, NATIVE handles and other root SOURCES are marked alive and others are garbage. In sweep phase, the heap is traversed to find gaps between live objects and the gaps are marked free list used for allocating memory to new objects.

52.

Which of the following has the highest memory requirement?(a) Heap(b) Stack(c) JVM(d) ClassI had been asked this question at a job interview.Question is taken from Heap and Garbage Collection in chapter Classes and Methods of Java

Answer» CORRECT ANSWER is (c) JVM

For EXPLANATION: JVM is the super SET which contains HEAP, stack, objects, pointers, etc.
53.

Where is a new object allocated memory?(a) Young space(b) Old space(c) Young or Old space depending on space availability(d) JVMThe question was posed to me in quiz.Question is from Heap and Garbage Collection in chapter Classes and Methods of Java

Answer»

Correct option is (a) Young space

Easiest explanation: A NEW object is ALWAYS created in young space. Once young space is full, a special young collection is RUN where objects which have LIVED long enough are moved to old space and memory is FREED up in young space for new objects.

54.

What would be the behaviour if one parameterized constructor is explicitly defined?(a) Compilation error(b) Compilation succeeds(c) Runtime error(d) Compilation succeeds but at the time of creating object using default constructor, it throws compilation errorThe question was asked in an international level competition.This intriguing question comes from Constructor topic in section Classes and Methods of Java

Answer»

The correct OPTION is (d) COMPILATION succeeds but at the time of creating OBJECT using default constructor, it throws compilation error

Explanation: The class compiles successfully. But the object creation of that class GIVES a compilation error.

55.

What is not the use of “this” keyword in Java?(a) Passing itself to another method(b) Calling another constructor in constructor chaining(c) Referring to the instance variable when local variable has the same name(d) Passing itself to method of the same classI had been asked this question in examination.This is a very interesting question from Constructor in division Classes and Methods of Java

Answer»

Right choice is (d) Passing itself to method of the same class

The best I can EXPLAIN: “this” is an important keyword in JAVA. It HELPS to distinguish between LOCAL VARIABLE and variables passed in the method as parameters.

56.

Abstract class cannot have a constructor.(a) True(b) FalseI have been asked this question in unit test.My enquiry is from Constructor topic in chapter Classes and Methods of Java

Answer»

The correct OPTION is (B) False

Explanation: No instance can be created of ABSTRACT class. Only pointer can hold instance of OBJECT.

57.

What is true about constructor?(a) It can contain return type(b) It can take any number of parameters(c) It can have any non access modifiers(d) Constructor cannot throw an exceptionThis question was posed to me in an international level competition.I would like to ask this question from Constructor topic in section Classes and Methods of Java

Answer»

The correct answer is (B) It can take any number of parameters

For explanation: Constructor returns a new object with variables defined as in the class. Instance variables are NEWLY created and only one copy of static variables are created.

58.

What is true about protected constructor?(a) Protected constructor can be called directly(b) Protected constructor can only be called using super()(c) Protected constructor can be used outside package(d) protected constructor can be instantiated even if child is in a different packageI got this question during an interview.Question is from Constructor in chapter Classes and Methods of Java

Answer»

Right choice is (b) Protected constructor can only be called using SUPER()

The best EXPLANATION: Protected access modifier means that constructor can be accessed by child CLASSES of the parent class and classes in the same PACKAGE.

59.

What is true about Class.getInstance()?(a) Class.getInstance calls the constructor(b) Class.getInstance is same as new operator(c) Class.getInstance needs to have matching constructor(d) Class.getInstance creates object if class does not have any constructorI have been asked this question in class test.The origin of the question is Constructor topic in chapter Classes and Methods of Java

Answer»

The CORRECT ANSWER is (d) Class.GETINSTANCE creates object if class does not have any constructor

The EXPLANATION is: Class class provides list of methods for use LIKE getInstance().

60.

What is false about constructor?(a) Constructors cannot be synchronized in Java(b) Java does not provide default copy constructor(c) Constructor can have a return type(d) “this” and “super” can be used in a constructorThis question was addressed to me in examination.I would like to ask this question from Constructor topic in chapter Classes and Methods of Java

Answer»

Correct ANSWER is (c) Constructor can have a return type

The explanation: The constructor cannot have a return type. It should create and return NEW objects. HENCE it WOULD give a compilation error.

61.

What is true about private constructor?(a) Private constructor ensures only one instance of a class exist at any point of time(b) Private constructor ensures multiple instances of a class exist at any point of time(c) Private constructor eases the instantiation of a class(d) Private constructor allows creating objects in other classesThis question was posed to me in quiz.Origin of the question is Constructor in portion Classes and Methods of Java

Answer»

The correct option is (a) Private CONSTRUCTOR ensures only one instance of a CLASS exist at any point of time

For explanation: OBJECT of private constructor can only be created within class. Private constructor is USED in singleton pattern.

62.

What would be the behaviour if this() and super() used in a method?(a) Runtime error(b) Throws exception(c) compile time error(d) Runs successfullyThe question was posed to me during a job interview.This is a very interesting question from Constructor in portion Classes and Methods of Java

Answer» RIGHT answer is (C) COMPILE time error

The EXPLANATION: this() and super() cannot be USED in a method. This throws compile time error.
63.

Which operator is used by Java run time implementations to free the memory of an object when it is no longer needed?(a) delete(b) free(c) new(d) none of the mentionedI got this question in semester exam.My question is based upon Constructors & Garbage Collection topic in portion Classes and Methods of Java

Answer»

Correct choice is (d) none of the mentioned

Explanation: Java handles DEALLOCATION of memory automatically, we do not need to explicitly delete an element. Garbage collection only OCCURS during execution of the program. When no references to the object exist, that object is ASSUMED to be no longer needed, and the memory occupied by the object can be reclaimed.

64.

Which function is used toperform some action when the object is to be destroyed?(a) finalize()(b) delete()(c) main()(d) none of the mentionedI had been asked this question in an online quiz.My doubt stems from Constructors & Garbage Collection in division Classes and Methods of Java

Answer» RIGHT choice is (a) FINALIZE()

Easiest EXPLANATION: NONE.
65.

Which of the following is a method having same name as that of its class?(a) finalize(b) delete(c) class(d) constructorI have been asked this question in examination.My question is based upon Constructors & Garbage Collection in division Classes and Methods of Java

Answer»

The correct choice is (d) CONSTRUCTOR

The explanation: A constructor is a method that initializes an OBJECT immediately UPON creation. It has the same name as that of class in which it resides.

66.

What is the return type of Constructors?(a) int(b) float(c) void(d) none of the mentionedThe question was asked in semester exam.My enquiry is from Constructors & Garbage Collection topic in division Classes and Methods of Java

Answer» RIGHT answer is (d) none of the mentioned

The explanation: CONSTRUCTORS does not have any RETURN TYPE, not even void.
67.

Which keyword is used by the method to refer to the object that invoked it?(a) import(b) catch(c) abstract(d) thisI had been asked this question in an interview.This question is from Constructors & Garbage Collection in section Classes and Methods of Java

Answer»

The correct answer is (d) this

The best I can explain: this KEYWORD can be used INSIDE any method to refer to the current OBJECT. this is always a reference to the object on which the method was INVOKED.

68.

What is the return type of a method that does not return any value?(a) int(b) float(c) void(d) doubleThis question was addressed to me in an interview for job.I'd like to ask this question from Introduction To Methods in portion Classes and Methods of Java

Answer»

The correct option is (C) VOID

To elaborate: RETURN type of a method MUST be MADE void if it is not returning any value.

69.

What is the process of defining more than one method in a class differentiated by method signature?(a) Function overriding(b) Function overloading(c) Function doubling(d) None of the mentionedI had been asked this question by my college director while I was bunking the class.My question is based upon Introduction To Methods in section Classes and Methods of Java

Answer»

Correct choice is (B) Function overloading

Easy explanation: Function overloading is a process of defining more than ONE method in a class with same name differentiated by function signature i:e return type or parameters type and number. Example –INT volume(int LENGTH, int width) & int volume(int length , int width , int height) can be used to CALCULATE volume.

70.

Which of the following is a method having same name as that of it’s class?(a) finalize(b) delete(c) class(d) constructorThe question was posed to me in class test.My question is from Introduction To Methods topic in chapter Classes and Methods of Java

Answer»

The CORRECT answer is (d) constructor

Best explanation: A constructor is a METHOD that initializes an object immediately upon CREATION. It has the same NAME as that of class in which it resides.

71.

Which of these operators is used to allocate memory for an object?(a) malloc(b) alloc(c) new(d) giveThis question was posed to me during an online interview.This interesting question is from Class Fundamentals & Declaring objects topic in chapter Classes and Methods of Java

Answer»

The correct choice is (C) new

The EXPLANATION is: Operator new dynamically ALLOCATES MEMORY for an object and returns a reference to it. This reference is address in memory of the object ALLOCATED by new.

72.

Which of these statement is incorrect?(a) Every class must contain a main() method(b) Applets do not require a main() method at all(c) There can be only one main() method in a program(d) main() method must be made publicThis question was posed to me in an online quiz.This interesting question is from Class Fundamentals & Declaring objects topic in division Classes and Methods of Java

Answer»

Right choice is (a) Every class MUST contain a main() method

To EXPLAIN I WOULD say: Every class does not need to have a main() method, there can be only one main() method which is made PUBLIC.

73.

Which of the following is a valid declaration of an object of class Box?(a) Box obj = new Box();(b) Box obj = new Box;(c) obj = new Box();(d) new Box obj;This question was addressed to me in a job interview.Query is from Class Fundamentals & Declaring objects topic in portion Classes and Methods of Java

Answer» CORRECT choice is (a) Box OBJ = new Box();

EXPLANATION: None.
74.

Which of these keywords is used to make a class?(a) class(b) struct(c) int(d) none of the mentionedThe question was asked during an interview for a job.My question is from Class Fundamentals & Declaring objects in chapter Classes and Methods of Java

Answer»

The CORRECT CHOICE is (a) class

Easy EXPLANATION: NONE.