Chapter 6_Inside classes and objects

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Answer: d) Error Explanation: Variable height is not defined. output: $ javac cons_method.java $ java cons_method error: cannot find symbol height

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); } } a) 0 b) 1 c) 30 d) error

c) 25

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); } } a) 0 b) 1 c) 25 d) 26

c) 6

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); } } a) 0 b) 1 c) 6 d) 25

Answer: d) Garbage value Explanation: Object obj of box class contains reference to the memory which was given to its class instances. Printing obj will print the address of the memory. output: $ javac mainclass.java $ java mainclass box@130671e

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); } } a) 0 b) 1 c) Runtime error d) Garbage value

b) 200

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); } } a) 12 b) 200 c) 400 d) 100

Answer: a) 1 Explanation: When we assign an object to another object of same type, all the elements of right side object gets copied to object on left side of equal to, =, operator.

What is the output of this program? class box { int width; int height; int length; } 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); } } a) 1 b) 2 c) Runtime error d) Garbage value

b) true

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()); } } a) false b) true c) 0 d) 1

Answer: c) Compilation error Explanation: Two variables with the same name can't be created in a class. output:

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); } } } a) 9 b) 8 c) Compilation error d) Runtime error

Answer: d) Compilation Error Explanation: main() method must be made public. Without main() being public java run time system will not be able to access main() and will not be able to execute the code. output: $ javac Output.java Error: Main method not found in class Output, please define the main method as: public static void main(String[] args)

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); } } a) 1 b) 2 c) Runtime Error d) Compilation Error

Answer: b) Function overloading Explanation: Function overloading is a process of defining more than one method in a class with same name differentiated by function signature i:e return type or parameters type and number. Example - int volume(int length, int width) & int volume(int length , int width , int height) can be used to calculate volume.

What is the process of defining more than one method in a class differentiated by method signature? a) Function overriding b) Function overloading c) Function doubling d) None of the mentioned

Answer: c) void Explanation: Return type of an method must be made void if it is not returning any value.

What is the return type of a method that does not returns any value? a) int b) float c) void d) double

Answer: b) Null Explanation: Memory is allocated to an object using new operator. box obj; just declares a reference to object, no memory is allocated to it hence it points to NULL.

What is the stored in the object obj in following lines of code? box obj; a) Memory address of allocated memory of object. b) NULL c) Any arbitrary pointer d) Garbage

Answer: a) main method Explanation: main() method can be defined only once in a program. Program execution begins from the main() method by java's run time system.

Which method can be defined only once in a program? a) main method b) finalize method c) static method d) private method

Answer: d) constructor Explanation: A constructor is a method that initializes an object immediately upon creation. It has the same name as that of class in which it resides.

Which of the following is a method having same name as that of it's class? a) finalize b) delete c) class d) constructor

a) Box obj = new Box();

Which of the following is a valid declaration of an object of class Box? a) Box obj = new Box(); b) Box obj = new Box; c) obj = new Box(); d) new Box obj;

a) Public method is accessible to all other classes in the hierarchy

Which of the following statements is correct? a) Public method is accessible to all other classes in the hierarchy b) Public method is accessible only to subclasses of its parent class c) Public method can only be called by object of its class. d) Public method can be accessed by calling object of the public class.

b) Object class

Which of these class is superclass of every class in Java? a) String class b) Object class c) Abstract class d) ArrayList class

b) abstract class

Which of these class relies upon its subclasses for complete implementation of its methods? a) Object class b) abstract class c) ArrayList class d) None of the mentioned

Answer: d) final Explanation: Declaring a class final implicitly declares all of its methods final, and makes the class inheritable.

Which of these keywords can be used to prevent inheritance of a class? a) super b) constant c) Class d) final

Answer: a) abstract Explanation: A abstract class is incomplete by itself and relies upon its subclasses to provide complete implementation. If we declare a class final then no class can inherit that class, an abstract class needs its subclasses hence both final and abstract cannot be used for a same class.

Which of these keywords cannot be used for a class which has been declared final? a) abstract b) extends c) abstract and extends d) None of the mentioned

a) class

Which of these keywords is used to make a class? a) class b) struct c) int d) None of the mentioned

Answer: c) new Explanation: Operator new dynamically allocates memory for an object and returns a reference to it. This reference is address in memory of the object allocated by new.

Which of these operators is used to allocate memory for an object? a) malloc b) alloc c) new d) give

Answer: d) All object of a class are allotted memory for the methods defined in the class. Explanation: All object of class share a single copy of methods defined in a class, Methods are allotted memory only once. All the objects of the class have access to methods of that class are allotted memory only for the variables not for the methods.

Which of these statement is incorrect? a) All object of a class are allotted memory for the all the variables defined in the class. b) If a function is defined public it can be accessed by object of other class by inheritation. c) main() method must be made public. d) All object of a class are allotted memory for the methods defined in the class.

Answer: a Explanation: Every class does not need to have a main() method, there can be only one main() method which is made public.

Which of these statement is incorrect? a) Every class must contain a main() method. b) Applets do not require a main() method at all. c) There can be only one main() method in a program. d) main() method must be made public.


Ensembles d'études connexes

Motivation and Emotion Exam 1 (chapter 1-3)

View Set

Secondary survey chapter 12 Arnheim pp, Types of shock, Medical Terminology, Injuries & Deformities of the hand, Budgeting terms, BOC Prep and NATA-BOC Exam Secrets Study Guide, BOC

View Set

ABEKA 9TH GRADE BIBLE KINGS OF ISRAEL SEMESTER VERSE EXAM(VERSES FOR MIDTERM)

View Set

Ch.7 Control of Microbial Growth

View Set

Maternal and Child Practice Exam 3

View Set

Inquizitive Chap 4: Civil Liberties

View Set