InterviewSolution
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.
| 1. |
final class Complex {private final double re;private final double im;public Complex(double re, double im) {this.re = re;this.im = im;}public String toString() {return "(" + re + " + " + im + "i)";}}class Main {public static void main(String args[]){Complex c = new Complex(10, 15);System.out.println("Complex number is " + c);}}(A)Complex number is (10.0 + 15.0i)(B) Compiler Error(C)Complex number is SOME_GARBAGE(D)Complex number is Complex8e2fb5Here 8e2fb5 is hash code of c |
| Answer» | |
| 2. |
Predict the output of following program. Note that foo() is public in base and private in derived.class Base {public void foo() { System.out.println("Base"); }}class Derived extends Base {private void foo() { System.out.println("Derived"); }}public class Main {public static void main(String args[]) {Base b = new Derived();b.foo();}}(A) Base(B) Derived(C) Compiler Error(D) Runtime Error |
| Answer» | |
| 3. |
Which of the following is true about inheritance in Java.1) In Java all classes inherit from the Object class directly or indirectly. The Object class is root of all classes.2) Multiple inheritance is not allowed in Java.3) Unlike C++, there is nothing like type of inheritance in Java where we can specify whether the inheritance is protected, public or private.(A) 1, 2 and 3(B) 1 and 2(C) 2 and 3(D) 1 and 3 |
| Answer» | |
| 4. |
class Base {final public void show() {System.out.println("Base::show() called");}}class Derived extends Base {public void show() {System.out.println("Derived::show() called");}}class Main {public static void main(String[] args) {Base b = new Derived();;b.show();}}(A) Base::show() called(B) Derived::show() called(C) Compiler Error(D) Runtime Error |
| Answer» | |
| 5. |
Output of following Java program?class Base {public void Print() {System.out.println("Base");}}class Derived extends Base {public void Print() {System.out.println("Derived");}}class Main{public static void DoPrint( Base o ) {o.Print();}public static void main(String[] args) {Base x = new Base();Base y = new Derived();Derived z = new Derived();DoPrint(x);DoPrint(y);DoPrint(z);}}(A)BaseDerivedDerived(B)BaseBaseDerived(C)BaseDerivedBase(D) Compiler Error |
| Answer» | |
| 6. |
class Base {public static void show() {System.out.println("Base::show() called");}}class Derived extends Base {public static void show() {System.out.println("Derived::show() called");}}class Main {public static void main(String[] args) {Base b = new Derived();b.show();}}(A) Base::show() called(B) Derived::show() called(C) Compiler Error |
| Answer» | |
| 7. |
Which of the following is true about inheritance in Java?1) Private methods are final.2) Protected members are accessible within a package and inherited classes outside the package.3) Protected methods are final.4) We cannot override private methods. (A) 1, 2 and 4(B) Only 1 and 2(C) 1, 2 and 3(D) 2, 3 and 4 |
| Answer» | |
| 8. |
What would be the result if a class extends two interfaces and both have a method with same name and signature? Lets assume that the class is not implementing that method.(a) Runtime error(b) Compile time error(c) Code runs successfully(d) First called method is executed successfullyThis question was posed to me in an online interview.Question is taken from Inheritance in chapter Inheritance of Java |
|
Answer» The correct choice is (b) COMPILE TIME error |
|
| 9. |
Does Java support multiple level inheritance?(a) True(b) FalseI had been asked this question during an online exam.This is a very interesting question from Inheritance topic in chapter Inheritance of Java |
|
Answer» Correct option is (a) True |
|
| 10. |
Which of the following is used for implementing inheritance through class?(a) inherited(b) using(c) extends(d) implementsI have been asked this question during an internship interview.I would like to ask this question from Inheritance topic in portion Inheritance of Java |
|
Answer» The CORRECT OPTION is (c) extends |
|
| 11. |
Which of the following is used for implementing inheritance through an interface?(a) inherited(b) using(c) extends(d) implementsI got this question in an internship interview.This question is from Inheritance in division Inheritance of Java |
|
Answer» Correct answer is (d) implements |
|
| 12. |
If super class and subclass have same variable name, which keyword should be used to use super class?(a) super(b) this(c) upper(d) classnameThe question was asked at a job interview.This interesting question is from Inheritance in portion Inheritance of Java |
|
Answer» CORRECT answer is (a) super For explanation I WOULD say: Super keyword is used to access HIDDEN super class VARIABLE in subclass. |
|
| 13. |
Static members are not inherited to subclass.(a) True(b) FalseThis question was addressed to me in quiz.My question is from Inheritance in division Inheritance of Java |
|
Answer» Right CHOICE is (B) False |
|
| 14. |
In order to restrict a variable of a class from inheriting to subclass, how variable should be declared?(a) Protected(b) Private(c) Public(d) StaticThe question was posed to me by my college director while I was bunking the class.This intriguing question originated from Inheritance in chapter Inheritance of Java |
|
Answer» The correct answer is (b) Private |
|
| 15. |
All classes in Java are inherited from which class?(a) java.lang.class(b) java.class.inherited(c) java.class.object(d) java.lang.ObjectI got this question in semester exam.My question is taken from Inheritance topic in portion Inheritance of Java |
|
Answer» Right option is (d) java.lang.OBJECT |
|
| 16. |
Using which of the following, multiple inheritance in Java can be implemented?(a) Interfaces(b) Multithreading(c) Protected methods(d) Private methodsI had been asked this question in semester exam.Question is from Inheritance in portion Inheritance of Java |
|
Answer» Right option is (a) INTERFACES |
|
| 17. |
What is not type of inheritance?(a) Single inheritance(b) Double inheritance(c) Hierarchical inheritance(d) Multiple inheritanceI had been asked this question during an interview.This interesting question is from Inheritance topic in division Inheritance of Java |
|
Answer» Right option is (b) DOUBLE inheritance |
|
| 18. |
A class member declared protected becomes a member of subclass of which type?(a) public member(b) private member(c) protected member(d) static memberThe question was posed to me in a national level competition.Origin of the question is Inheritance topic in chapter Inheritance of Java |
|
Answer» The CORRECT OPTION is (b) private MEMBER |
|
| 19. |
A class member declared protected becomes a member of subclass of which type?(a) public member(b) private member(c) protected member(d) static memberThis question was addressed to me during a job interview.This key question is from Inheritance topic in chapter Inheritance of Java |
| Answer» | |
| 20. |
If a class inheriting an abstract class does not define all of its function then it will be known as?(a) Abstract(b) A simple class(c) Static class(d) None of the mentionedI got this question during an interview for a job.Asked question is from Inheritance topic in section Inheritance of Java |
|
Answer» Correct choice is (a) Abstract |
|
| 21. |
Which of this keyword must be used to inherit a class?(a) super(b) this(c) extent(d) extendsThe question was posed to me during an online interview.I want to ask this question from Inheritance in chapter Inheritance of Java |
|
Answer» CORRECT ANSWER is (d) extends For EXPLANATION: NONE. |
|
| 22. |
Which of these is not a correct statement?(a) Every class containing abstract method must be declared abstract(b) Abstract class defines only the structure of the class not its implementation(c) Abstract class can be initiated by new operator(d) Abstract class can be inheritedI had been asked this question during an online interview.The query is from Inheritance topic in section Inheritance of Java |
|
Answer» The CORRECT choice is (c) Abstract class can be initiated by new OPERATOR |
|
| 23. |
Which of these keywords are used to define an abstract class?(a) abst(b) abstract(c) Abstract(d) abstract classThe question was asked by my college professor while I was bunking the class.This intriguing question originated from Inheritance in division Inheritance of Java |
|
Answer» The CORRECT answer is (B) abstract |
|
| 24. |
Which of these is not abstract?(a) Thread(b) AbstractList(c) List(d) None of the MentionedI had been asked this question during a job interview.This key question is from Inheritance topic in division Inheritance of Java |
|
Answer» Correct CHOICE is (a) Thread |
|
| 25. |
Which of these keywords cannot be used for a class which has been declared final?(a) abstract(b) extends(c) abstract and extends(d) none of the mentionedThe question was posed to me in an online interview.I'm obligated to ask this question of The Object Class topic in section Inheritance of Java |
|
Answer» The correct answer is (a) abstract |
|
| 26. |
Which of these keywords can be used to prevent inheritance of a class?(a) super(b) constant(c) class(d) finalThis question was posed to me during an internship interview.The origin of the question is The Object Class topic in portion Inheritance of Java |
|
Answer» The correct CHOICE is (d) final |
|
| 27. |
Which of these method of Object class is used to obtain class of an object at run time?(a) get()(b) void getclass()(c) Class getclass()(d) None of the mentionedThe question was posed to me in my homework.Question is taken from The Object Class topic in chapter Inheritance of Java |
|
Answer» CORRECT ANSWER is (C) Class getclass() Best EXPLANATION: None. |
|
| 28. |
Which of these method of Object class can clone an object?(a) Objectcopy()(b) copy()(c) Object clone()(d) clone()I got this question in semester exam.The question is from The Object Class in division Inheritance of Java |
|
Answer» The correct choice is (C) Object CLONE() |
|
| 29. |
Which of these class is superclass of every class in Java?(a) String class(b) Object class(c) Abstract class(d) ArrayList classI had been asked this question in an interview for internship.I'm obligated to ask this question of The Object Class in division Inheritance of Java |
|
Answer» Correct OPTION is (b) Object CLASS |
|
| 30. |
Which of these keywords can be used to prevent Method overriding?(a) static(b) constant(c) protected(d) finalThis question was addressed to me in unit test.I'm obligated to ask this question of Method overriding in chapter Inheritance of Java |
|
Answer» The correct OPTION is (d) final |
|
| 31. |
Which of these is correct way of calling a constructor having no parameters, of superclass A by subclass B?(a) super(void);(b) superclass.();(c) super.A();(d) super();I had been asked this question during an interview for a job.My question is from Method overriding topic in chapter Inheritance of Java |
|
Answer» RIGHT answer is (d) SUPER(); BEST explanation: NONE. |
|
| 32. |
What is the process of defining a method in a subclass having same name & type signature as a method in its superclass?(a) Method overloading(b) Method overriding(c) Method hiding(d) None of the mentionedThis question was posed to me in unit test.My enquiry is from Method overriding topic in portion Inheritance of Java |
|
Answer» CORRECT ANSWER is (B) METHOD overriding Explanation: NONE. |
|
| 33. |
Which of this keyword can be used in a subclass to call the constructor of superclass?(a) super(b) this(c) extent(d) extendsThe question was asked in an online quiz.The doubt is from Method overriding in chapter Inheritance of Java |
|
Answer» RIGHT ANSWER is (a) super Easy EXPLANATION: NONE. |
|