QuizzesJava Classes & Objects
beginner
java

Java Classes & Objects

Understand how to create classes, instantiate objects, use constructors, and work with the this keyword.

25 minutes 20 questions Pass at 70%
Time Limit
25 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

Questions in this quiz

20 questions on Java Classes & Objects. Work through them here, then start the quiz to answer them and see your score.

  1. 1.What is a class in Java?

    • AA running instance of a program
    • BA group of methods only
    • CA type of variable
    • DA blueprint for creating objects
  2. 2.What is an object in Java?

    • AThe same thing as a class
    • BAn instance of a class created in memory
    • CA file that contains code
    • DA method that returns a value
  3. 3.What is a constructor in Java?

    • AA method that destroys objects
    • BA method that returns the class name
    • CA special method that runs when an object is created
    • DA method that copies an object
  4. 4.What is the output?

    class Cat {
        String name;
        Cat(String n) { this.name = n; }
    }
    Cat c = new Cat("Whiskers");
    System.out.println(c.name);
    • ACompilation error
    • Bnull
    • CWhiskers
    • Dn
  5. 5.What does the this keyword refer to?

    • AThe current object instance
    • BThe current method
    • CThe main class of the program
    • DThe parent class
  6. 6.What is a field (instance variable) in a class?

    • AA method inside the class
    • BA class inside another class
    • CA variable that belongs to each object of the class
    • DThe name of the class
  7. 7.What is the output?

    class Counter {
        int count = 0;
        void increment() { count++; }
    }
    Counter c = new Counter();
    c.increment();
    c.increment();
    System.out.println(c.count);
    • A0
    • B2
    • C3
    • D1
  8. 8.What is method overloading?

    • AHaving too many methods in one class
    • BMultiple methods with the same name but different parameters
    • CA method that calls itself
    • DCalling a method twice in a row
  9. 9.What is a static field in Java?

    • AA field that holds static text
    • BA field shared by ALL objects of the class
    • CA field that is only used in constructors
    • DA field that cannot be changed
  10. 10.What is the output?

    class Dog {
        static int count = 0;
        Dog() { count++; }
    }
    new Dog();
    new Dog();
    new Dog();
    System.out.println(Dog.count);
    • ACompilation error
    • B0
    • C1
    • D3
  11. 11.What happens if a class has no constructor defined?

    • AYou cannot create objects from that class
    • BJava provides a default no-argument constructor automatically
    • CCompilation error
    • DThe class uses the constructor from Object class only
  12. 12.What is a getter method?

    • AA method that creates objects
    • BA method that accepts user input
    • CA method that deletes a field
    • DA method that reads (returns) the value of a private field
  13. 13.What is the output?

    class Person {
        private String name;
        Person(String name) { this.name = name; }
        String getName() { return name; }
    }
    Person p = new Person("Alice");
    System.out.println(p.getName());
    • ACompilation error
    • Bname
    • CAlice
    • Dnull
  14. 14.What is the difference between a class and an object?

    • AA class is the blueprint; an object is the real thing made from that blueprint
    • BObjects can have methods but classes cannot
    • CClasses are faster than objects
    • DThere is no difference
  15. 15.What does the new keyword do?

    • AImports a new class
    • BCreates a new object in memory and calls the constructor
    • CCreates a new file
    • DDeclares a new variable
  16. 16.What is a static method?

    • AA method that only works with static variables
    • BA method that never changes
    • CA method that belongs to the class and can be called without creating an object
    • DA method that cannot be called
  17. 17.What is the output?

    class MathUtil {
        static int square(int n) { return n * n; }
    }
    System.out.println(MathUtil.square(5));
    • A10
    • B25
    • CCompilation error
    • D5
  18. 18.Can a class have multiple constructors?

    • AOnly if they are static
    • BNo, only one constructor is allowed
    • CYes, but only two
    • DYes, as long as they have different parameter lists
  19. 19.What does the toString() method do in a class?

    • ADeletes the object
    • BConverts fields to static
    • CReturns a String representation of the object
    • DConverts the class name to lowercase
  20. 20.What is the output?

    class Box {
        int width, height;
        Box(int w, int h) { width = w; height = h; }
        int area() { return width * height; }
    }
    Box b = new Box(4, 5);
    System.out.println(b.area());
    • A20
    • BCompilation error
    • C9
    • D18