0

Simple Java Quiz

Description: Simple Java Quiz
Number of Questions: 10
Created by:
Tags: java
Attempted 0/10 Correct 0 Score 0
  1. A. Only Static methods of the same class

  2. B. Only instances of the same class

  3. C. Only methods those defined in the same class

  4. D. Only classes available in the same package.


Correct Option: C
  1. A. An instance of a class is an object

  2. C. Object is the super class of all other classes

  3. D. Objects do not permit encapsulation


Correct Option: C

What is an aggregate object?

  1. A. An object with only primitive attributes

  2. C. An instance which has other objects

  3. D. None of the above


Correct Option: B

What is the meaning of the return data type void?

  1. B. void returns no data type.

  2. C. void is not supported in Java

  3. D. None of the above


Correct Option: A

A class can have many methods with the same name as long as the number of parameters or type of parameters is different. This OOP concept is known as

  1. A. Method Invocating

  2. B. Method Overriding

  3. C. Method Labeling

  4. D. Method Overloading


Correct Option: D

AI Explanation

To answer this question, we need to understand the concept of method overloading in object-oriented programming (OOP).

Option A) Method Invocating - This option is incorrect because there is no such concept in OOP called "Method Invocating."

Option B) Method Overriding - This option is incorrect because method overriding refers to the ability of a subclass to provide a different implementation of a method that is already defined in its superclass.

Option C) Method Labeling - This option is incorrect because there is no such concept in OOP called "Method Labeling."

Option D) Method Overloading - This option is correct. Method overloading is the ability to define multiple methods with the same name within a class, as long as the number or type of parameters is different. This allows for flexibility and code reusability by providing different ways to invoke a method based on the arguments passed.

The correct answer is D) Method Overloading.

After the following code fragment, what is the value in fname?String str;int fname;str = ";Foolish boy.";fname = str.indexOf("fool");

  1. A. 0

  2. B. 2

  3. C. -1

  4. D. 4


Correct Option: C
Explanation:

To determine the value of fname after the given code fragment, we need to understand the indexOf() method in Java and how it is used.

The indexOf() method in Java returns the index of the first occurrence of a specified substring within a given string. It returns -1 if the substring is not found.

Now let's go through the code fragment and evaluate each line:

String str;
int fname;
str = ";Foolish boy.";
fname = str.indexOf("fool");
  • String str; declares a variable str of type String.
  • int fname; declares a variable fname of type int.
  • str = ";Foolish boy."; assigns the string ";Foolish boy." to the variable str.
  • fname = str.indexOf("fool"); assigns the return value of indexOf("fool") to the variable fname.

Now, let's evaluate each option and explain why it is right or wrong:

A. 0: This option is incorrect. The indexOf("fool") method call will return -1 because the substring "fool" is not found in the string str = ";Foolish boy.". Therefore, fname will be assigned the value -1, not 0.

B. 2: This option is incorrect. The indexOf("fool") method call will return -1 because the substring "fool" is not found in the string str = ";Foolish boy.". Therefore, fname will be assigned the value -1, not 2.

C. -1: This option is correct. The indexOf("fool") method call will return -1 because the substring "fool" is not found in the string str = ";Foolish boy.". Therefore, fname will be assigned the value -1.

D. 4: This option is incorrect. The indexOf("fool") method call will return -1 because the substring "fool" is not found in the string str = ";Foolish boy.". Therefore, fname will be assigned the value -1, not 4.

Therefore, the correct answer is:

C. -1

The code snippetif( "Welcome".trim() == "Welcome".trim() )System.out.println("Equal");elseSystem.out.println("Not Equal");will

  1. A. Compile and display “Equal”

  2. B. Compile and display “Not Equal”

  3. C. Cause a compiler error

  4. D. Compile and display NULL


Correct Option: A

Which of the following is not a return type?

  1. A. boolean

  2. B. void

  3. C. public

  4. D. Button


Correct Option: C

The class java.lang.Exception is

  1. A. protected

  2. B. extends Throwable

  3. C. implements Throwable

  4. D. serializable


Correct Option: B

AI Explanation

To answer this question, we need to understand the characteristics of the java.lang.Exception class.

Option A) A. protected - This option is incorrect because the java.lang.Exception class is not declared as protected. The protected access modifier restricts access to the class to only its subclasses and classes in the same package.

Option B) B. extends Throwable - This option is correct because the java.lang.Exception class extends the Throwable class. In Java, all exception classes, including the Exception class, are subclasses of the Throwable class.

Option C) C. implements Throwable - This option is incorrect because the java.lang.Exception class does not implement the Throwable interface. It extends the Throwable class directly.

Option D) D. serializable - This option is incorrect because the java.lang.Exception class does not implement the Serializable interface. The Serializable interface is used for enabling an object to be serialized and deserialized.

The correct answer is B) B. extends Throwable. This option is correct because the java.lang.Exception class is a subclass of the Throwable class.

- Hide questions