Sie sind auf Seite 1von 6

PRACTICE 2

1. Arrays in Java are implemented as? 5. void cal(int a, int b){


6. x += a ;
a) class
7. y += b;
b) object
8. }
c) variable 9. }
d) None of the mentioned 10. class static_specifier {
2. Which of these keywords is used to prevent 11. public static void main(String args[])
12. {
content of a variable from being modified?
13. access obj1 = new access();
a) final
14. access obj2 = new access();
b) last 15. obj1.x = 0;
c) constant 16. obj1.y = 0;
d) static 17. obj1.cal(1, 2);
18. obj2.x = 0;
3. Which of these cannot be declared static?
19. obj2.cal(2, 3);
a) class
20. System.out.println(obj1.x + " " + obj2.y);
b) object 21. }
c) variable 22. }
d) method a) 1 2
4. Which of the following statements are incorrect? b) 2 3
a) static methods can call other static methods only. c) 3 2
b) static methods must only access static data. d) 1 5
c) static methods can not refer to this or super in any 8. What is the output of this program?
way. 1. class access{
d) when object of class is declared, each object 2. static int x;
contains its own copy of static variables. 3. void increment(){
5. Which of the following statements are incorrect? 4. x++;
5. }
a) Variables declared as final occupy memory.
6. }
b) final variable must be initialized at the time of 7. class static_use {
declaration. 8. public static void main(String args[])
c) Arrays in java are implemented as an object. 9. {
d) All arrays contain an attribute-length which 10. access obj1 = new access();
11. access obj2 = new access();
contains the number of elements stored in the array.
12. obj1.x = 0;
6. Which of these methods must be made static? 13. obj1.increment();
a) main() 14. obj2.increment();
b) delete() 15. System.out.println(obj1.x + " " + obj2.x);
c) run() 16. }
17. }
d) finalize()
a) 1 2
7. What is the output of this program?
b) 1 1
2. class access{
c) 2 2
3. public int x;
4. static int y;
PRACTICE 2
d) Compilation Error 5. int a2[] = {1, 2, 3, 4, 5};
6. System.out.println(a1.length + " " + a2.length);
9. What is the output of this program?
7. }
1. class static_out { 8. }
2. static int x; a) 10 5
3. static int y;
b) 5 10
4. void add(int a , int b){
5. x = a + b; c) 0 10
6. y = x + b; d) 0 5
7. }
12. Which of these keyword must be used to inherit a class?
8. }
9. class static_use { a) super
10. public static void main(String args[]) b) this
11. { c) extent
12. static_out obj1 = new static_out();
d) extends
13. static_out obj2 = new static_out();
13. Which of these keywords is used to refer to member
14. int a = 2;
15. obj1.add(a, a + 1); of base class from a sub class?
16. obj2.add(5, a); a) upper
17. System.out.println(obj1.x + " " + obj2.y); b) super
18. }
c) this
19. }
d) None of the mentioned
a) 7 7
14. A class member declared protected becomes member of
b) 6 6
subclass of which type?
c) 7 9
a) public member
d) 9 7
b) private member
10. What is the output of this program?
c) protected member
1. class Output { d) static member
2. public static void main(String args[])
15. Which of these is correct way of inheriting class A by
3. {
4. int arr[] = {1, 2, 3, 4, 5}; class B?
5. for ( int i = 0; i < arr.length - 2; ++i) a) class B + class A {}
6. System.out.println(arr[i] + " "); b) class B inherits class A {}
7. } c) class B extends A {}
8. }
d) class B extends class A {}
a) 1 2
16. Which of the following statements are incorrect?
b) 1 2 3
a) public members of class can be accessed by any code in the
c) 1 2 3 4
program.
d) 1 2 3 4 5
b) private members of class can only be accessed by other
11. What is the output of this program?
members of the class.
1. class Output { c) private members of class can be inherited by a sub class,
2. public static void main(String args[])
and become protected members in sub class.
3. {
d) protected members of a class can be inherited by a sub
4. int a1[] = new int[10];
PRACTICE 2
class, and become private members of the sub class. 17. obj.display();
18. }
17. What is the output of this program?
19. }
1. class A { a) 2 2
2. int i;
b) 3 3
3. void display() {
4. System.out.println(i); c) 2 3
5. } d) 3 2
6. } 19. What is the output of this program?
7. class B extends A {
1. class A {
8. int j;
2. public int i;
9. void display() {
3. private int j;
10. System.out.println(j);
4. }
11. }
5. class B extends A {
12. }
6. void display() {
13. class inheritance_demo {
7. super.j = super.i + 1;
14. public static void main(String args[])
8. System.out.println(super.i + " " + super.j);
15. {
9. }
16. B obj = new B();
10. }
17. obj.i=1;
11. class inheritance {
18. obj.j=2;
12. public static void main(String args[])
19. obj.display();
13. {
20. }
14. B obj = new B();
21. }
15. obj.i=1;
a) 0
16. obj.j=2;
b) 1 17. obj.display();
c) 2 18. }
d) Compilation Error 19. }

18. What is the output of this program? a) 2 2


b) 3 3
1. class A {
2. int i; c) Runtime Error
3. } d) Compilation Error
4. class B extends A { 20. What is the output of this program?
5. int j;
1. class A {
6. void display() {
2. public int i;
7. super.i = j + 1;
3. public int j;
8. System.out.println(j + " " + i);
4. A() {
9. }
5. i = 1;
10. }
6. j = 2;
11. class inheritance {
7. }
12. public static void main(String args[])
8. }
13. {
9. class B extends A {
14. B obj = new B();
10. int a;
15. obj.i=1;
11. B() {
16. obj.j=2;
12. super();
PRACTICE 2
13. } d) None of the mentioned
14. }
15. class super_use {
16. public static void main(String args[]) 23. Which keyword is used by method to refer to the object
17. { that invoked it?
18. B obj = new B();
a) import
19. System.out.println(obj.i + " " + obj.j)
20. } b) catch
21. } c) abstract
a) 1 2 d) this
b) 2 1 24. Which of the following is a method having same name as
c) Runtime Error that of its class?
d) Compilation Error a) finalize
21. What is the output of this program? b) delete
c) class
1. class A {
2. public int i; d) constructor
3. protected int j;
25. Which operator is used by Java run time implementations
4. }
5. class B extends A { to free the memory of an object when it is no longer needed?
6. int j; a) delete
7. void display() { b) free
8. super.j = 3; c) new
9. System.out.println(i + " " + j);
d) None of the mentioned
10. }
11. } 26. Which function is used to perform some action when the
12. class Output { object is to be destroyed?
13. public static void main(String args[]) a) finalize()
14. { b) delete()
15. B obj = new B();
c) main()
16. obj.i=1;
17. obj.j=2; d) None of the mentioned
18. obj.display(); 27. What is the output of this program?
19. }
1. class box {
20. }
2. int width;
a) 1 2 3. int height;
b) 2 1 4. int length;
c) 1 3 5. int volume;
6. box() {
d) 3 1
7. width = 5;
22 What is the return type of Constructors? 8. height = 5;
a) int 9. length = 6;
10. }
b) float
11. void volume() {
c) void 12. volume = width*height*length;
13. }
PRACTICE 2
14. } 11. volume = width*height*length;
15. class constructor_output { 12. System.out.println(volume);
16. public static void main(String args[]) 13. }
17. { 14. }
18. box obj = new box(); 15. class Output {
19. obj.volume(); 16. public static void main(String args[])
20. System.out.println(obj.volume); 17. {
21. } 18. box obj = new box();
22. } 19. obj.volume();
a) 100 20. }
21. }
b) 150
a) 150
c) 200
b) 200
d) 250
c) Runtime error
28. What is the output of this program?
d) Compilation error
1. class equality {
30. Which of the following statements are incorrect?
2. int x;
3. int y; a) Default constructor is called at the time of declaration of the
4. boolean isequal() { object if a constructor has not been defined.
5. return(x == y); b) Constructor can be parameterized.
6. } c) finalize() method is called when a object goes out of scope
7. }
and is no longer needed.
8. class Output {
9. public static void main(String args[]) d) finalize() method must be declared protected. int a =
10. { 10;
11. equality obj = new equality();
1. int b = 20;
12. obj.x = 5;
2. obj.meth(a , b);
13. obj.y = 5;
3. System.out.println(a + " " + b);
14. System.out.println(obj.isequal); }
4. }
15. }
5. }
a) false
a) 10 20
b) true
b) 20 10
c) 0
c) 20 40
d) 1
d) 40 20
29. What is the output of this program?
41. What is the output of this program?
1. class box {
1. class test {
2. int width;
2. int a;
3. int height;
3. int b;
4. int length;
4. test(int i, int j) {
5. int volume;
5. a = i;
6. void finalize() {
6. b = j;
7. volume = width*height*length;
7. }
8. System.out.println(volume);
8. void meth(test o) {
9. }
9. o.a *= 2;
10. protected void volume() {
PRACTICE 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

Das könnte Ihnen auch gefallen