Study

PR1-W6-Part1-OOP_Classes&Objects

  •   0%
  •  0     0     0

  • 16. Given String s1 = new String("hi"); String s2 = "hi";, what is s1 == s2? (3)
    B (new String("hi") creates a separate object on the heap; s2 references the interned literal)
  • 6. What is the output? (2)
    5 (int is primitive → assignment copies the value. Changing y does not affect x.)
  • 5. Which of the following best describes encapsulation? (2)
    B (Encapsulation uses private fields and public getters/setters or methods to control access)
  • 7. What is the output? (2)
    Fido (d1 and d2 refer to the same object → changing through one affects the other.)
  • 2. Which of the following is not a primitive type in Java? (1)
    C. String
  • 15. Given String s1 = "hi"; String s2 = "hi";, what is s1 == s2? (3)
    B ( String literals are interned; identical literals refer to the same object in the string pool, so == is true)
  • 20. Predict the output: (4)
    Hello from B (Dynamic dispatch — method chosen at runtime based on actual object type, not reference type)
  • Won 25 points
    25
  • 23. Which of these is not part of the typical class structure? (1)
    D (A class describes fields, methods, constructors. Memory addresses are runtime details, not class structure)
  • 9. What is printed? (3)
    70.0 (Withdraw subtracts from balance, leaving 70)
  • 8. What happens? (3)
    Compile error (No default constructor is available since the class declared a constructor with parameters)
  • 1. Which Java keyword is used to create a new object (an instance) of a class? (1)
    C. new (Example: Dog d = new Dog();)
  • 18.What will be printed? (4)
    10 (Constructor assigns value; method prints it)
  • 11. Which keyword refers to the current instance inside an instance method or constructor? (2)
    C. this
  • 10. What is the output? (3)
    2 (Static field count is shared across all objects. Each constructor increments once)
  • 13. What does the expression a == b test when a and b are object references? (3)
    B ( == compares references for objects. equals() can be overridden to compare content.)
  • 14. Which statement about equals() is correct? (3)
    B (Object.equals() tests this == obj. Many classes (e.g., String) override it to compare content)
  • 21. Which of the following is a valid reason to make a field private and use getters/setters? (2)
    C (Private fields prevent arbitrary external modification; setters can validate values)
  • 3. What does a class represent in object-oriented programming? (1)
    B (A class defines fields and methods. Instances (objects) are created from that blueprint.)
  • 24. Which access modifier allows visibility only within the same class? (2)
    C
  • 12. Which is true about static fields? (2)
    B (Only one copy of a static field exists per class loader; all instances share it)
  • 4. Which statement about the main method is correct? (2)
    B (JVM calls public static void main(String[] args) without creating an object, so main is static)
  • Won 5 points
    5
  • 17.Which line causes compile error? (3)
    The System.out.println(x); line (x is an instance field; main is static → cannot access directly without an object)