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++ Java Basics of Java
Attempted 0/15 Correct 0 Score 0

Which of the following is the correct format of a Java program?

  1.   class abc {
       Public static void main(String args[]) {……………}
      }
    
  2.   class abc {
       public Static void main(String args[]) {……………}
      }
    
  3.   class abc {
       Public static void Main(String args[]) {……………}
      }
    
  4.   class abc {
       public static void main(String args[]) {……………}
      }
    

Correct Option: D
Explanation:

The correct format for a Java program is as follows: class abc { public static void main(String args[]) { …………… } } Here, the keywords class, public and static must be in lower case. String is the class name.

Which of the following statements regarding 'finally' block in Java is false?

  1. Before the termination of the program, JVM executes the 'finally' block (if any).

  2. The 'finally' block can be used for writing the cleanup code.

  3. The 'finally' block must be followed by try or catch block.

  4. There can be multiple 'finally' blocks for a try block.

  5. The 'finally' block is not executed if the program exists.


Correct Option: D
Explanation:

This is a false statement. There can only be one finally block for each try block. Since the statement is false about 'finally' block in Java, it is the correct alternative.

Which of the following methods defined in Math class is used to return the smallest whole number greater than or equal to the argument specified in it?

  1. abs()

  2. round()

  3. ceil()

  4. floor()

  5. max()


Correct Option: C
Explanation:

This method is used to return the smallest whole number greater than or equal to the argument specified in it.

Which of the following statements is false regarding inheritence?

  1. A class can extend another class.

  2. A class can implement multiple interfaces.

  3. A class can extend multiple classes.

  4. An interface can extend another interface.

  5. A class can extend another class and implement an interface at the same time.


Correct Option: C
Explanation:

This is a false statement. Multiple inheritence is not supported in Java.

Which of the following is the Super class of all the classes in Java?

  1. Class

  2. Throwable

  3. Object

  4. String

  5. Super


Correct Option: C
Explanation:

It is the correct answer. Object is the Super class of all classes in Java.

How many parameterized constructors are defined for a Scanner class?

  1. 5

  2. 6

  3. 7

  4. 8


Correct Option: D
Explanation:

There are 8 parameterized constructors defined for a Scanner class.

Which of the following methods of Observable class returns the true value if the invoking object is modified and false if it is not?

  1. countObservers()

  2. deleteObserver()

  3. hasChanged()

  4. setChanged()

  5. addObserver()


Correct Option: C
Explanation:

This method of Observable class returns true value if the invoking object has been modified and false if it has not.

Which of the following statements is not correct about a constructor?

  1. A constructor can be executed simultaneously with the creation of an object.

  2. A constructor which is used for one object cannot be used again unless a second object is created.

  3. A constructor does not return a value.

  4. A constructor cannot be overloaded.

  5. A constructor must have the same name as its class.


Correct Option: D
Explanation:

Since the statement is true about constructors, it the wrong answer. Since the statement is not true about constructors, it  is the correct answer.

What will be the output of java MyClass,java? class Base {
public static void main(String[] args) {
System.out.println(In Base Class );
}
}
public class MyClass extends Base { }

  1. compile time error

  2. run time error

  3. execute successfully with no output

  4. prints In Base Class

  5. none of these


Correct Option: D
Explanation:

When we run stand alone java program, the main method of that class is called. If main method is not present in that class, java runtime system calls the inherited method from parent class.

Find the output of the following program code. class parent {
final int a=10;
void meth() {
System.out.println(parent); } } class Test1 extends parent { int a=20;
public static void main(String[] args)
{ parent par = new parent(); parent par2 = new Test1(); par.meth(); par2.meth(); System.out.println(par.a); System.out.println(par2.a); } void meth() { System.out.println(child);
}
}

  1. parentchild1010

  2. parentchild 2020

  3. child child2020

  4. parentparent 2020

  5. childchild1010


Correct Option: A
Explanation:

par is the object of parent class so it will call meth() method of parent class, par2 will call the mothod of Test1 class due to overriding but the variable will call only of Parent class because overriding is for methods not for variable

Consider the following program code. class person { private string name; private int age; void person() { name=Raju; } talk() { System.out.println(Hello I am + name); } } Which constructor has been wrongly defined?

  1. class person()

  2. talk()

  3. person()

  4. private int age;

  5. none of these


Correct Option: C
Explanation:

This is a constructor since the class is also person and the constructors have the same names as that of their classes. It is wrongly defined in the program as void is used before constructor which is not allowed.

The constructor StringBuffer() of a StringBuffer class creates a string buffer with the capacity of ________ characters by default.

  1. 8

  2. 16

  3. 32

  4. 64


Correct Option: B
Explanation:

The constructor StringBuffer() of a StringBuffer class creates a StringBuffer with the capacity of 16 characters by default.

What is the correct way to access the static methods declared within the class?

  1. Static methods can be accessed using the class name.

  2. Static methods can be accessed directly by writing the method name.

  3. Static methods can be accessed using objects of the class, where the static method is defined.

  4. Static methods can be accessed using “this”.


Correct Option: A
Explanation:

Static methods can be accessed using the class name. Syntax: Classname.Static_method();

Which of the following are constructors for the wrapper Byte class?

  1. Byte(Byte b1);
    Byte(String str1);
  2. Byte(int b1);
    Byte(String str1);
  3. Byte(byte b1);
    Byte(String str1);
  4. Byte(byte b1);
    Byte(string str1);

Correct Option: C
Explanation:

There are two constructors for the Byte class.

1. Byte(byte b1);

  1. Byte(String str1);

The class String is defined in the package ___________.

  1. java.awt

  2. java.lang

  3. java.math

  4. java.net


Correct Option: B
Explanation:

The class String is defined in the package java.lang.

- Hide questions