Java Class and Object Fundamentals
24 Questions
100 Views

Java Class and Object Fundamentals

Created by
@DetachableHydra

Questions and Answers

What is stored in the object obj in the following lines of code? box obj;

  • Any arbitrary pointer
  • NULL (correct)
  • Garbage
  • Memory address of allocated memory of object
  • Which of these keywords is used to create a class?

  • int
  • None of the mentioned
  • class (correct)
  • struct
  • Which of the following is a valid declaration of an object of class Box?

  • Box obj = new Box;
  • obj = new Box();
  • Box obj = new Box(); (correct)
  • new Box obj;
  • Which of these operators is used to allocate memory for an object?

    <p>new</p> Signup and view all the answers

    Which of these statements is incorrect?

    <p>Every class must contain a main() method.</p> Signup and view all the answers

    What is the output of this program? class main_class { public static void main(String args[]) { int x = 9; if (x == 9) { int x = 8; System.out.println(x); } } }

    <p>Compilation error</p> Signup and view all the answers

    Which of the following statements is correct?

    <p>Public method is accessible to all other classes in the hierarchy.</p> Signup and view all the answers

    What is the output of this program? class box { int width; int height; int length; } class mainclass { public static void main(String args[]) { box obj = new box(); obj.width = 10; obj.height = 2; obj.length = 10; int y = obj.width * obj.height * obj.length; System.out.print(y); } }

    <p>200</p> Signup and view all the answers

    What is the output of this program? class mainclass { public static void main(String args[]) { box obj1 = new box(); box obj2 = new box(); obj1.height = 1; obj1.length = 2; obj1.width = 1; obj2 = obj1; System.out.println(obj2.height); } }

    <p>1</p> Signup and view all the answers

    What is the output of this program? class box { int width; int height; int length; } class mainclass { public static void main(String args[]) { box obj = new box(); System.out.println(obj); } }

    <p>Garbage value</p> Signup and view all the answers

    What is the return type of a method that does not return any value?

    <p>void</p> Signup and view all the answers

    What is the process of defining more than one method in a class differentiated by method signature?

    <p>Function overloading</p> Signup and view all the answers

    Which of the following is a method having the same name as that of its class?

    <p>constructor</p> Signup and view all the answers

    Which method can be defined only once in a program?

    <p>main method</p> Signup and view all the answers

    Which of these statements is incorrect?

    <p>All objects of a class are allotted memory for the methods defined in the class.</p> Signup and view all the answers

    What is the output of this program? class box { int width; int height; int length; int volume; void volume(int height, int length, int width) { volume = width*height*length; } } class Prameterized_method{ public static void main(String args[]) { box obj = new box(); obj.height = 1; obj.length = 5; obj.width = 5; obj.volume(3,2,1); System.out.println(obj.volume); } }

    <p>6</p> Signup and view all the answers

    What is the output of this program? class equality { int x; int y; boolean isequal(){ return(x == y); } } class Output { public static void main(String args[]) { equality obj = new equality(); obj.x = 5; obj.y = 5; System.out.println(obj.isequal()); } }

    <p>true</p> Signup and view all the answers

    What is the output of this program? class box { int width; int height; int length; int volume; void volume() { volume = width*height*length; } } class Output { public static void main(String args[]) { box obj = new box(); obj.height = 1; obj.length = 5; obj.width = 5; obj.volume(); System.out.println(obj.volume); } }

    <p>25</p> Signup and view all the answers

    What is the output of this program? class Output { static void main(String args[]) { int x , y = 1; x = 10; if (x != 10 && x / 0 == 0) System.out.println(y); else System.out.println(++y); } }

    <p>Compilation Error</p> Signup and view all the answers

    What is the output of this program? class area { int width; int length; int volume; area() { width=5; length=6; } void volume() { volume = width*length*height; } } class cons_method { public static void main(String args[]) { area obj = new area(); obj.volume(); System.out.println(obj.volume); } }

    <p>error</p> Signup and view all the answers

    Which of these classes is the superclass of every class in Java?

    <p>Object class</p> Signup and view all the answers

    Which of these keywords can be used to prevent inheritance of a class?

    <p>final</p> Signup and view all the answers

    Which of these keywords cannot be used for a class that has been declared final?

    <p>abstract</p> Signup and view all the answers

    Which of these classes relies upon its subclasses for complete implementation of its methods?

    <p>abstract class</p> Signup and view all the answers

    Study Notes

    Class and Object Fundamentals

    • Memory allocation for an object occurs with the new operator; declaring a reference (e.g., box obj;) does not allocate memory, leading to a pointer to NULL.
    • The keyword class is used to define a class in Java.

    Object Declaration

    • The correct syntax for declaring an object of the Box class is Box obj = new Box();.

    Memory Allocation Operator

    • The operator new is used to dynamically allocate memory for an object and returns a reference to its address in memory.

    Main Method Requirements

    • Not every class is required to contain a main() method; only one public main() method is permitted in a Java program.

    Variable Naming Rules

    • Two variables with the same name cannot coexist in a class; attempting to do so results in a compilation error.

    Access Modifiers

    • Public methods are accessible from any class in the hierarchy, allowing broader access than methods restricted to subclasses.

    Program Outputs

    • In an example program, a Box object's dimensions lead to an output of 200 when multiplied (width: 10, height: 2, length: 10).
    • Assigning one object to another copies all elements, thus returning the expected value if both objects are of the same type.

    Object Reference Behavior

    • When an object is printed directly, it displays its memory address rather than its properties, resulting in a "Garbage value" output.

    Method Return Types

    • A method that does not return a value must have a return type of void.

    Function Overloading

    • Function overloading allows multiple methods in a class to share the same name, provided they differ in parameters or return types.

    Constructors

    • A constructor is a special method that initializes an object upon creation and carries the same name as the class.

    Single Main Method Rule

    • The main() method serves as the program's execution entry point and can only be defined once in any Java program.

    Memory Allocation for Methods

    • All objects of a class share a single copy of the methods, as they utilize memory for variables but not for methods themselves.

    Checking for Equality

    • An equality check method can return a boolean indicating if two properties are equal, which can be demonstrated in a simple program.

    Abstract and Final Classes

    • A class declared as final cannot be subclassed, and thus no methods can be abstract. Abstract classes, however, require subclasses for complete implementation of their methods.

    Superclass in Java

    • The Object class is the root superclass for all classes in Java, providing foundational methods.

    Inheritance Restrictions

    • The final keyword is used to prevent inheritance from a class, ensuring that no subclass can extend the functionality of that class.

    Abstract Class Functionality

    • An abstract class lacks a complete implementation and relies on its subclasses for defining concrete methods, differing from regular classes which can be fully defined.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Description

    This quiz covers the essential concepts of classes and objects in Java. Learn about memory allocation, object declaration, and access modifiers. Perfect for beginners seeking to understand Java's object-oriented programming principles.

    More Quizzes Like This

    Java Programming Quiz
    10 questions

    Java Programming Quiz

    HumorousFluorite avatar
    HumorousFluorite
    Java Programming Principles
    18 questions
    Java Programming Fundamentals
    10 questions

    Java Programming Fundamentals

    AdroitNovaculite8015 avatar
    AdroitNovaculite8015
    Computer Science Test IV Flashcards
    10 questions
    Use Quizgecko on...
    Browser
    Browser