0

Simple Java Quiz

Description: Simple Java Quiz
Number of Questions: 10
Created by:
Tags: java
Attempted 0/10 Correct 0 Score 0

Which methods can access to private attributes of a class

  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

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) Only Static methods of the same class - This option is incorrect. Static methods of the same class cannot directly access private attributes of a class. Private attributes are only accessible within the class they are defined in.

Option B) Only instances of the same class - This option is incorrect. Instances of the same class cannot directly access private attributes of a class. Private attributes are only accessible within the class they are defined in.

Option C) Only methods those defined in the same class - This option is correct. Private attributes can be accessed by other methods defined in the same class. Since private attributes are only accessible within the class, any method defined within the class can access and modify these private attributes.

Option D) Only classes available in the same package - This option is incorrect. Private attributes are not directly accessible by classes available in the same package. Private attributes are only accessible within the class they are defined in.

The correct answer is C. Only methods defined in the same class can access private attributes of a class.

Which of the following statements is false about objects

  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

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) A. An instance of a class is an object - This option is correct. In object-oriented programming, an object is an instance of a class. When you create an object, you are creating a specific instance of that class.

Option B) C. Object is the super class of all other classes - This option is correct. In many object-oriented programming languages, including Java, Object is the root class from which all other classes are derived. This means that all classes inherit properties and methods from the Object class.

Option C) D. Objects do not permit encapsulation - This option is incorrect. Encapsulation is one of the fundamental principles of object-oriented programming. It allows objects to encapsulate data and behavior within a single entity, providing data hiding and abstraction. Objects provide encapsulation by combining data and methods together.

The correct answer is C. Objects do permit encapsulation, so the statement "Objects do not permit encapsulation" is false.

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

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.

Consider the following code snippet. What will be assigned to the variable fourthChar, if the code is executed? String str = new String(“Java”); char fourthChar = str.charAt(4);

  1. A. ‘a’

  2. B. ‘v’

  3. C. throws StringIndexOutofBoundsException

  4. D. null characater


Correct Option: C

AI Explanation

To answer this question, we need to understand the charAt() method and how it works in Java.

The charAt() method is used to return the character at a specified index position in a string. In Java, string indexes are zero-based, which means the first character is at index 0, the second character is at index 1, and so on.

In the given code snippet, we have the following line:

char fourthChar = str.charAt(4);

Here, we are trying to access the character at index 4 of the string str. However, the string "Java" has a length of only 4 characters (indexes 0 to 3). Therefore, when we try to access the character at index 4, it will throw a StringIndexOutOfBoundsException.

Therefore, the correct answer is:

C. throws StringIndexOutOfBoundsException.

- Hide questions