LearnFrenzy
Sign in Take a free test

Classes and Objects · Exercise 1 of 4

Classes and Objects Classes and Objects MCQ

  • 20 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Twenty multiple-choice questions on Java classes and objects: classes vs objects, new, references, null, == vs equals, toString, object arrays, garbage collection and nested classes. Pick an option to check your answer instantly.

Question 1 Easy

Which statement best describes the relationship between a class and an object?

A They are the same thing
B A class is a blueprint; an object is an instance created from it at run time
C An object is a blueprint for classes
D A class is an object stored in a file
Question page & discussion
Question 2 Easy

What does new Car() do?

A Declares a variable
B Creates a Car object on the heap and returns a reference to it
C Copies an existing Car
D Imports the Car class
Question page & discussion
Question 3 Easy

How many Car objects does Car c1, c2, c3; create?

A 3
B 0
C 1
D It depends on the constructor
Question page & discussion
Question 4 Easy

After Car b = a;, how many Car objects exist (assuming a referred to one)?

A 2
B 1 – both variables refer to the same object
C 0
D It depends
Question page & discussion
Question 5 Easy

What does it mean when a reference variable is null?

A It refers to an empty object
B It refers to no object at all
C It refers to zero
D It is uninitialised memory
Question page & discussion
Question 6 Easy

Which line throws NullPointerException if s is null?

A if (s == null)
B s.length()
C String t = s;
D System.out.println(s);
Question page & discussion
Question 7 Easy

A class has fields int age; String name; boolean active;. What are they right after new (no constructor code)?

A Random values
B 0, null, false
C 0, "", false
D Compile error
Question page & discussion
Question 8 Medium

If a class does not override equals, what does a.equals(b) compare?

A All fields
B The references – same as a == b
C The hash codes
D The toString results
Question page & discussion
Question 9 Medium

Why must hashCode be overridden whenever equals is?

A For speed
B Equal objects must return equal hash codes, or hash-based collections like HashMap and HashSet will not find them
C The compiler requires it
D toString uses it
Question page & discussion
Question 10 Easy

What does System.out.println(obj) print for a class that does not override toString?

A All field values
B The class name, @ and a hexadecimal hash code
C Nothing
D Compile error
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links