menu

Java Classes and Objects


1. Which of the following is not a valid identifier for a class in Java?

MyClass

myclass

my_class

myClass123


2. Which of the following is true about static variables in Java?

They are created when an object is created.

They are stored in the heap memory.

They are shared among all objects of a class.

They can be accessed using object reference.


3. Which keyword is used to prevent a method from being overridden in a subclass in Java?

final

static

abstract

synchronized


4.

Which of the following is not a valid Java identifier for a class?

MyClass

myClass

My_Class

1MyClass


5. Which of the following is a valid declaration for a method that takes two integer arguments and returns an integer in Java?

public int myMethod(int x, int y) {}

public myMethod(int x, int y) {}

public int myMethod(int x; int y) {}

public int myMethod(int x int y) {}


6. Which of the following is true about the Object class in Java?

It is the parent class of all other classes in Java.

It is an abstract class.

It provides default implementations for several methods.

All of the above.


7. Which of the following is not a method in the Object class in Java?

toString()

hashCode()

equals()

clone()


8. Which method is used to compare two objects for equality in Java?

toString()

hashCode()

equals()

compareTo()


9. Which of the following is true about inheritance in Java?

It allows a class to inherit the properties of another class.

It allows a class to inherit the properties of an object.

It allows an object to inherit the properties of a class.

It allows an object to inherit the properties of another object.


10. Which method is called when an object is created?

constructor

main

finalize

toString