0

Constructors, Classes, and Inheritance

Description: Constructors, Classes, and Inheritance
Number of Questions: 15
Created by:
Tags: Constructors Classes and Inheritance Java/Oracle /C/C++ Basics of Java Inheritance and Constructor
Attempted 0/15 Correct 0 Score 0

The protected access specifier works as ________ for outside world, but works as ________ for derived classes in Java.

  1. private and public

  2. public and private

  3. private and private

  4. public and public


Correct Option: A
Explanation:

The protected access specifier works as private for outside world, but works as public for derived classes in Java.

If the first statement of a constructor has the form ________, then the constructor calls another constructor of the same class.

  1. static

  2. this(parameter list)

  3. this

  4. static(parameter list)


Correct Option: B
Explanation:

If the first statement of a constructor has the form this(parameter list), then the constructor calls another constructor of the same class. For example: class Emp {     int m;     double n;     public Emp(int x)     {             this(789.89,x);      } } Here, this(789.89,x) statement calls the constructor Emp(double,int)

The protected access specifier is very useful in ___________ inheritance.

  1. simple

  2. multiple

  3. multilevel

  4. hierarchical


Correct Option: C
Explanation:

The protected access specifier is very useful in multilevel inheritance because the protected member of super class A can be accessed by the subclass C in the following way. A(Super Class)$\rightarrow$ B(Subclass of A) $\rightarrow$C(Subclass of B)

Which of the following classes is not a wrapper class?

  1. Float

  2. Integer

  3. Double

  4. String

  5. Byte


Correct Option: D
Explanation:

This is a correct answer. String is not a wrapper class in java.

Final modifier can be applied to

  1. classes only

  2. methods only

  3. data members only

  4. classes, methods and data members

  5. interfaces only


Correct Option: D
Explanation:

This is a wrong answer. Interfaces cannot be marked as final. This is the correct answer. Final keyword can be used with classes, methods and data members.

All the data members in an interface are

  1. integers

  2. final

  3. protected

  4. private

  5. string


Correct Option: B
Explanation:

This is the correct answer. All data members inside an interface need to be final. Once the data members are final, their values cannot be changed. If we do not make the data members final, it will result in compile time error.

Select a valid answer with respect to the following piece of code. class A {
int value1; } class B extends A {
int value2; }

  1. Class A extends class B.

  2. Class B is superclass of class A.

  3. Class A inherits from class B.

  4. Class B is subclass of class A.

  5. Class A is subclass of class B.


Correct Option: D
Explanation:

This is the correct answer. As class B is extending from class A, hence class A is the superclass and class B is the subclass.

Which of the following statements is/are correct in Java? A. Constructors do not have any return type. B. Constructors cannot be private.

  1. Only A

  2. Only B

  3. Both A and B

  4. Neither A nor B


Correct Option: C
Explanation:

Both are the properties of a constructor. A constructor does not have any return type and cannot be declared as private.

If there are more than one constructors in a class, then it is termed as ____________.

  1. multi-constructor

  2. default Constructor

  3. constructor overloading

  4. constructor overriding


Correct Option: C
Explanation:

If there are more than one constructor in a class, then its termed as constructor overloading.

What is the minimum number of parameters in a constructor of Java?

  1. 0

  2. 1

  3. 2

  4. 3


Correct Option: A
Explanation:

A constructor can be without a parameter also. The constructor without any parameter is called a default constructor.

Which type of inheritance is represented by the following code? class A { } class B extends A { }

  1. Simple inheritance

  2. Multilevel inheritance

  3. Multiple inheritance

  4. Hierarchical inheritance


Correct Option: A
Explanation:

The given structure is a representation of simple inheritance.

Which type of inheritance is represented by the following code? class A { } class B extends A { } class C extends B { }

  1. Simple inheritance

  2. Multilevel inheritance

  3. Multiple inheritance

  4. Hierarchical inheritance


Correct Option: B
Explanation:

The given structure is a representation of multilevel inheritance.

Multiple inheritance can be implemented in Java using __________.

  1. classes

  2. interfaces

  3. structures

  4. static methods


Correct Option: B
Explanation:

Multiple inheritance can be implemented in Java using interfaces. One or more classes can implement the defined interface.

A __________ class cannot be extended.

  1. public

  2. private

  3. abstract

  4. final


Correct Option: D
Explanation:

A final class cannot be extended. If we try the compiler will give an error.

Which of the following is the implementation of the class structure given below?

  1. class A

    { } class B { } class C implements A,B { }|

  2. class A

    { } class B { } class C implement A,B { }|

  3. class A

    { } class B { } class C extends A,B { }|

  4. class A

    { } class B { } class C extend A,B { }|


Correct Option: A
Explanation:

The implementation in Java for the given structure is as follows: class A { } class B { } class C implements A,B { }

- Hide questions