Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java Mcqs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

1. 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 classes
View Answer

2. 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 successfully
View Answer

3. What is false about constructor?


a) Constructors cannot be synchronized in Java
b) Java does not provide default copy constructor
c) Constructor can be overloaded
d) “this” and “super” can be used in a constructor
View Answer

4. 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 constructor
View Answer

5. 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 exception
View Answer

6. Abstract class cannot have a constructor.


a) True
b) False
View Answer

7. 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 package
View Answer
advertisement

8. 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 class
View Answer

9. 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 error
View Answer

10. What would be behaviour if the constructor has a return type?


a) Compilation error
b) Runtime error
c) Compilation and runs successfully
d) Only String return type is allowed

1. Which of this keyword must be used to inherit a class?


a) super
b) this
c) extent
d) extends
2. A class member declared protected becomes a member of subclass of which type?
a) public member
b) private member
c) protected member
d) static member
3. Which of these is correct way of inheriting class A by class B?
a) class B + class A {}
b) class B inherits class A {}
c) class B extends A {}
d) class B extends class A {}

1. What is not type of inheritance?


a) Single inheritance
b) Double inheritance
c) Hierarchical inheritance
d) Multiple inheritance
View Answer
2. Using which of the following, multiple inheritance in Java can be implemented?
a) Interfaces
b) Multithreading
c) Protected methods
d) Private methods
View Answer
3. All classes in Java are inherited from which class?
a) java.lang.class
b) java.class.inherited
c) java.class.object
d) java.lang.Object
View Answer
4. 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) Static
View Answer
5. 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) classname
View Answer
6. Static members are not inherited to subclass.
a) True
b) False
View Answer
7. Which of the following is used for implementing inheritance through an interface?
a) inherited
b) using
c) extends
d) implements
View Answer
8. Which of the following is used for implementing inheritance through class?
a) inherited b) using c) extends d) implements

1. Which of this keyword can be used in a subclass to call the constructor of superclass?
a) super
b) this
c) extent
d) extends
View Answer

2. 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 mentioned
View Answer

3. Which of these keywords can be used to prevent Method overriding?


a) static
b) constant
c) protected
d) final
View Answer

4. 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();
View Answer

5. At line number 2 below, choose 3 valid data-type attributes/qualifiers among “final, static, native,
public, private, abstract, protected”

1. public interface Status


2. {
3. /* insert qualifier here */ int MY_VALUE = 10;
4. }

a) final, native, private


b) final, static, protected
c) final, private, abstract
d) final, static, public
View Answer

6. Which of these is supported by method overriding in Java?


a) Abstraction
b) Encapsulation
c) Polymorphism
d) None of the mentioned
View Answer

7. What is the output of this program?


1. class Alligator
2. {
3. public static void main(String[] args)
4. {
5. int []x[] = {{1,2}, {3,4,5}, {6,7,8,9}};
6. int [][]y = x;
7. System.out.println(y[2][1]);
8. }
9. }

a) 2
b) 3
c) 7
d) Compilation Error
View Answer

8. What is the output of this program?


advertisement

1. final class A
2. {
3. int i;
4. }
5. class B extends A
6. {
7. int j;
8. System.out.println(j + " " + i);
9. }
10. class inheritance
11. {
12. public static void main(String args[])
13. {
14. B obj = new B();
15. obj.display();
16. }
17. }

a) 2 2
b) 3 3
c) Runtime Error
d) Compilation Error
View Answer
9. What is the output of this program?

1. class Abc
2. {
3. public static void main(String[]args)
4. {
5. String[] elements = { "for", "tea", "too" };
6. String first = (elements.length > 0) ? elements[0]: null;
7. }
8. }

a) Compilation error
b) An exception is thrown at run time
c) The variable first is set to null
d) The variable first is set to elements[0].
View Answer

You might also like