Questions and Answers
What is stored in the object obj in the following lines of code? box obj;
Which of these keywords is used to create a class?
Which of the following is a valid declaration of an object of class Box?
Which of these operators is used to allocate memory for an object?
Signup and view all the answers
Which of these statements is incorrect?
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); } } }
Signup and view all the answers
Which of the following statements is correct?
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); } }
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); } }
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); } }
Signup and view all the answers
What is the return type of a method that does not return any value?
Signup and view all the answers
What is the process of defining more than one method in a class differentiated by method signature?
Signup and view all the answers
Which of the following is a method having the same name as that of its class?
Signup and view all the answers
Which method can be defined only once in a program?
Signup and view all the answers
Which of these statements is incorrect?
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); } }
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()); } }
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); } }
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); } }
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); } }
Signup and view all the answers
Which of these classes is the superclass of every class in Java?
Signup and view all the answers
Which of these keywords can be used to prevent inheritance of a class?
Signup and view all the answers
Which of these keywords cannot be used for a class that has been declared final?
Signup and view all the answers
Which of these classes relies upon its subclasses for complete implementation of its methods?
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 publicmain()
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.
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.