Classes and Methods

1- 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


2- 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;


3- Which of these operators is used to allocate memory for an object?


a) malloc
b) alloc
c) new
d) give


4- 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.


 5- What is the output of this program?

 

  1.     class main_class {

  2.         public static void main(String args[])

  3.         {

  4.             int x = 9;

  5.             if (x == 9) { 

  6.                 int x = 8;

  7.                 System.out.println(x);

  8.             }

  9.         } 

  10.     }

a) 9
b) 8
c) Compilation error
d) Runtime error


6- 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.


7- What is the output of this program?
  1.     class box {

  2.         int width;

  3.         int height;

  4.         int length;

  5.     } 

  6.     class mainclass {

  7.         public static void main(String args[]) 

  8.         {        

  9.              box obj = new box();

  10.              obj.width = 10;

  11.              obj.height = 2;

  12.              obj.length = 10;

  13.              int y = obj.width * obj.height * obj.length; 

  14.              System.out.print(y);

  15.         } 

  16.     }

a) 12
b) 200
c) 400
d) 100


8- What is the output of this program?

 

  1.    class box {

  2.         int width;

  3.         int height;

  4.         int length;

  5.     } 

  6.     class mainclass {

  7.         public static void main(String args[]) 

  8.         {        

  9.             box obj1 = new box();

  10.             box obj2 = new box();

  11.             obj1.height = 1;

  12.             obj1.length = 2;

  13.             obj1.width = 1;

  14.             obj2 = obj1;

  15.             System.out.println(obj2.height);

  16.         } 

  17.     }

a) 1
b) 2
c) Runtime error
d) Garbage value


9- 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

10- 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.


11- What is the output of this program?

 

  1.    class box {

  2.         int width;

  3.         int height;

  4.         int length;

  5.         int volume;

  6.         void volume(int height, int length, int width) {

  7.              volume = width*height*length;

  8.         } 

  9.     }    

  10.     class Prameterized_method{

  11.         public static void main(String args[])

  12.         {

  13.             box obj = new box();

  14.             obj.height = 1;

  15.             obj.length = 5;

  16.             obj.width = 5;

  17.             obj.volume(3,2,1);

  18.             System.out.println(obj.volume);        

  19.         } 

  20.     }

a) 0
b) 1
c) 6
d) 25


12- What is the output of this program?

 

  1.     class equality {

  2.         int x;

  3.         int y;

  4.         boolean isequal(){

  5.             return(x == y);  

  6.         } 

  7.     }    

  8.     class Output {

  9.         public static void main(String args[])

  10.         {

  11.             equality obj = new equality();

  12.             obj.x = 5;

  13.             obj.y = 5;

  14.             System.out.println(obj.isequal());

  15.         } 

  16.     }

a) false
b) true
c) 0
d) 1


13- What is the output of this program?

 

  1.     class box {

  2.         int width;

  3.         int height;

  4.         int length;

  5.         int volume;

  6.         void volume() {

  7.              volume = width*height*length;

  8.         } 

  9.     }    

  10.     class Output { 

  11.         public static void main(String args[])

  12.         {

  13.             box obj = new box();

  14.             obj.height = 1;

  15.             obj.length = 5;

  16.             obj.width = 5;

  17.             obj.volume();

  18.             System.out.println(obj.volume);        

  19.         } 

  20.     }

a) 0
b) 1
c) 25
d) 26


14- What is the output of this program?

 

  1.    class area {

  2.         int width;

  3.         int length;

  4.         int volume;

  5.         area() {

  6.         width=5;

  7.         length=6;

  8.         }

  9.         void volume() {

  10.              volume = width*length*height;

  11.         } 

  12.     }    

  13.     class cons_method {

  14.         public static void main(String args[])

  15.         {

  16.             area obj = new area();

  17.             obj.volume();

  18.             System.out.println(obj.volume);        

  19.         } 

  20.     }

a) 0
b) 1
c) 30
d) error


15- What is the return type of Constructors?


a) int
b) float
c) void
d) None of the mentioned


16- Which operator is used by Java run time implementations to free the memory of an object when it is no longer needed?


a) delete
b) free
c) new
d) None of the mentioned


17- Which function is used to perform some action when the object is to be destroyed?


a) finalize()
b) delete()
c) main()
d) None of the mentioned


18- What is the output of this program?

 

  1.     class equality {

  2.         int x;

  3.         int y;

  4.         boolean isequal() {

  5.             return(x == y);  

  6.         } 

  7.     }    

  8.     class Output {

  9.         public static void main(String args[])

  10.         {

  11.             equality obj = new equality();

  12.             obj.x = 5;

  13.             obj.y = 5;

  14.             System.out.println(obj.isequal);        } 

  15.     }

a) false
b) true
c) 0
d) 1


19-  Which of the folowing stements are incorrect?


a) Default constructor is called at the time of declaration of the object if a constructor has not been defined.
b) Constructor can be parameterized.
c) finalize() method is called when a object goes out of scope and is no longer needed.
d) finalize() method must be declared protected.


20- What is the output of this program?

 

  1.     class area {

  2.         int width;

  3.         int length;

  4.         int area;

  5.         void area(int width, int length) {

  6.             this.width = width;

  7.             this.length = length;

  8.         }

  9.  

  10.     }    

  11.     class Output {

  12.         public static void main(String args[])

  13.         {

  14.             area obj = new area();

  15.             obj.area(5 , 6);

  16.             System.out.println(obj.length + " " + obj.width);        

  17.         } 

  18.     }

a) 0 0
b) 5 6
c) 6 5
d) 5 5


21- Which of these can be overloaded?


a) Methods
b) Constructors
c) All of the mentioned
d) None of the mentioned


22- What is the process of defining a method in terms of itself, that is a method that calls itself?


a) Polymorphism
b) Abstraction
c) Encapsulation
d) Recursion


23- What is the output of this program?

 

  1.     class overload {

  2.         int x;

  3.   int y;

  4.         void add(int a) {

  5.             x =  a + 1;

  6.         }

  7.         void add(int a, int b){

  8.             x =  a + 2;

  9.         }        

  10.     }    

  11.     class Overload_methods {

  12.         public static void main(String args[])

  13.         {

  14.             overload obj = new overload();   

  15.             int a = 0;

  16.             obj.add(6);

  17.             System.out.println(obj.x);     

  18.         }

  19.    }

a) 5
b) 6
c) 7
d) 8


24- What is the output of this program?

 

  1.    class overload {

  2.         int x;

  3.   double y;

  4.         void add(int a , int b) {

  5.             x = a + b;

  6.         }

  7.         void add(double c , double d){

  8.             y = c + d;

  9.         }

  10.         overload() {

  11.             this.x = 0;

  12.             this.y = 0;

  13.         }        

  14.     }    

  15.     class Overload_methods {

  16.         public static void main(String args[])

  17.         {

  18.             overload obj = new overload();   

  19.             int a = 2;

  20.             double b = 3.2;

  21.             obj.add(a, a);

  22.             obj.add(b, b);

  23.             System.out.println(obj.x + " " + obj.y);     

  24.         }

  25.    }

a) 6 6
b) 6.4 6.4
c) 6.4 6
d) 4 6.4


25- What is the output of this program?

 

  1.     class test {

  2.         int a;

  3.         int b;

  4.         test(int i, int j) {

  5.             a = i;

  6.             b = j;

  7.         }

  8.         void meth(test o) {

  9.             o.a *= 2;

  10.             O.b /= 2;

  11.         }          

  12.     }    

  13.     class Output {

  14.         public static void main(String args[])

  15.         {

  16.             test obj = new test(10 , 20);

  17.             obj.meth(obj);

  18.             System.out.println(obj.a + " " + obj.b);        

  19.         } 

  20.     }

a) 10 20
b) 20 10
c) 20 40
d) 40 20


26- What is the output of this program?

 

  1.     class access{

  2.         public int x;

  3.   private int y;

  4.         void cal(int a, int b){

  5.             x =  a + 1;

  6.             y =  b;

  7.         }        

  8.     }    

  9.     class access_specifier {

  10.         public static void main(String args[])

  11.         {

  12.             access obj = new access();   

  13.             obj.cal(2, 3);

  14.             System.out.println(obj.x + " " + obj.y);     

  15.         }

  16.    }

a) 3 3
b) 2 3
c) Runtime Error
d) Compilation Error


27- What is the output of this program?

 

  1.     class static_out {

  2.         static int x;

  3.   static int y;

  4.         void add(int a, int b){

  5.             x = a + b;

  6.             y = x + b;

  7.         }

  8.     }    

  9.     class static_use {

  10.         public static void main(String args[])

  11.         {

  12.             static_out obj1 = new static_out();

  13.             static_out obj2 = new static_out();   

  14.             int a = 2;

  15.             obj1.add(a, a + 1);

  16.             obj2.add(5, a);

  17.             System.out.println(obj1.x + " " + obj2.y);     

  18.         }

  19.    }

a) 7 7
b) 6 6
c) 7 9
d) 9 7


Which of these method of String class is used to obtain character at specified index?
a) char()
b) Charat()
c) charat()
d) charAt()


 Which of these keywords is used to refer to member of base class from a sub class?
a) upper
b) super
c) this
d) None of the mentioned


Which of these method of String class can be used to test to strings for equality?
a) isequal()
b) isequals()
c) equal()
d) equals()


Which of the following statements are incorrect?
a) String is a class.
b) Strings in java are mutable.
c) Every string is an object of class String.
d) Java defines a peer class of String, called StringBuffer, which allows string to be altered.


What is the output of this program?

  1.     class string_class {
  2.         public static void main(String args[])
  3.         {
  4.             String obj = "hello";
  5.             String obj1 = "world";   
  6.             String obj2 = "hello";
  7.             System.out.println(obj.equals(obj1) + " " + obj.equals(obj2));
  8.         }
  9.     }

a) false false
b) true true
c) true false
d) false true


click to view answer

a

Related Articles

post a comment