Practice Set 1 With Ans
Practice Set 1 With Ans
Practice Set 1 With Ans
Total : 50Ques
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
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
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
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. System.out.println(obj);
11. }
12. }
a) 0
b) 1
c) Runtime error
d) classname@hashcode in hexadecimal form
11. What is the return type of a method that does not returns any value?
a) int
b) float
c) void
d) double
12. 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
13. Which of the following is a method having same name as that of it’s class?
a) finalize
b) delete
c) class
d) constructor
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
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
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
1. class Output {
2.
3. public static int sum(int ...x)
4. {
5. return;
6. }
7. static void main(String args[])
8. {
9. sum(10);
10. sum(10,20);
11. sum(10,20,30);
12. sum(10,20,30,40);
13. }
14. }
a) only sum(10)
b) only sum(10,20)
c) only sum(10) & sum(10,20)
d) all of the mentioned
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
23. Which of the following loops will execute the body of loop even when condition controlling
the loop is initially false?
a) do-while
b) while
c) for
d) None of the mentioned
24. Which of these jump statements can skip processing remainder of code in its body for a
particular iteration?
a) break
b) return
c) exit
d) continue
1. class selection_statements {
2. public static void main(String args[])
3. {
4. int var1 = 5;
5. int var2 = 6;
6. if ((var2 = 1) == var1)
7. System.out.print(var2);
8. else
9. System.out.print(++var2);
10. }
11. }
a) 1
b) 2
c) 3
d) 4
1. class comma_operator {
2. public static void main(String args[])
3. {
4. int sum = 0;
5. for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1)
6. sum += i;
7. System.out.println(sum);
8. }
9. }
a) 5
b) 6
c) 14
d) compilation error
1. class jump_statments {
2. public static void main(String args[])
3. {
4. int x = 2;
5. int y = 0;
6. for ( ; y < 10; ++y) {
7. if (y % x == 0)
8. continue;
9. else if (y == 8)
10. break;
11. else
12. System.out.print(y + " ");
13. }
14. }
15. }
a) 1 3 5 7
b) 2 4 6 8
c) 1 3 5 7 9
d) 1 2 3 4 5 6 7 8 9
1. class Output {
2. public static void main(String args[])
3. {
4. final int a=10,b=20;
5. while(a<b)
6. {
7.
8. System.out.println("Hello");
9. }
10. System.out.println("World");
11.
12. }
13. }
a) Hello
b) run time error
c) Hello world
d) compile time error
1. class Output {
2. public static void main(String args[])
3. {
4. int a = 5;
5. int b = 10;
6. first: {
7. second: {
8. third: {
9. if (a == b >> 1)
10. break second;
11. }
12. System.out.println(a);
13. }
14. System.out.println(b);
15. }
16. }
17. }
a) 5 10
b) 10 5
c) 5
d) 10
31. Which of these operators is used to allocate memory to array variable in Java?
a) malloc
b) alloc
c) new
d) new malloc
1. class array_output {
2. public static void main(String args[])
3. {
4. int array_variable [] = new int[10];
5. for (int i = 0; i < 10; ++i) {
6. array_variable[i] = i;
7. System.out.print(array_variable[i] + " ");
8. i++;
9. }
10. }
11. }
a) 0 2 4 6 8
b) 1 3 5 7 9
c) 0 1 2 3 4 5 6 7 8 9
d) 1 2 3 4 5 6 7 8 9 10
1. class multidimention_array {
2. public static void main(String args[])
3. {
4. int arr[][] = new int[3][];
5. arr[0] = new int[1];
6. arr[1] = new int[2];
7. arr[2] = new int[3];
8. int sum = 0;
9. for (int i = 0; i < 3; ++i)
10. for (int j = 0; j < i + 1; ++j)
11. arr[i][j] = j + 1;
12. for (int i = 0; i < 3; ++i)
13. for (int j = 0; j < i + 1; ++j)
14. sum + = arr[i][j];
15. System.out.print(sum);
16. }
17. }
a) 11
b) 10
c) 13
d) 14
1. class evaluate {
2. public static void main(String args[])
3. {
4. int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
5. int n = 6;
6. n = arr[arr[n] / 2];
7. System.out.println(arr[n] / 2);
8. }
9. }
a) 3
b) 0
c) 6
d) 1
1. class array_output {
2. public static void main(String args[])
3. {
4. char array_variable [] = new char[10];
5. for (int i = 0; i < 10; ++i) {
6. array_variable[i] = 'i';
7. System.out.print(array_variable[i] + "");
8. }
9. }
10. }
a) 1 2 3 4 5 6 7 8 9 10
b) 0 1 2 3 4 5 6 7 8 9 10
c) i j k l m n o p q r
d) i i i i i i i i i i
1. class array_output {
2. public static void main(String args[])
3. {
4. int array_variable[][] = {{ 1, 2, 3}, { 4 , 5, 6}, { 7, 8,
9}};
5. int sum = 0;
6. for (int i = 0; i < 3; ++i)
7. for (int j = 0; j < 3 ; ++j)
8. sum = sum + array_variable[i][j];
9. System.out.print(sum / 5);
10. }
11. }
a) 8
b) 9
c) 10
d) 11
43. A class member declared protected becomes member of subclass of which type?
a) public member
b) private member
c) protected member
d) static member
45. Which two classes use the Shape class correctly? (Choose two.)
a) B,E
b) A,C
c) C,E
d) T,H
1. class A {
2. int i;
3. void display() {
4. System.out.println(i);
5. }
6. }
7. class B extends A {
8. int j;
9. void display() {
10. System.out.println(j);
11. }
12. }
13. class inheritance_demo {
14. public static void main(String args[])
15. {
16. B obj = new B();
17. obj.i=1;
18. obj.j=2;
19. obj.display();
20. }
21. }
a) 0
b) 1
c) 2
d) Compilation Error
1. class A {
2. int i;
3. }
4. class B extends A {
5. int j;
6. void display() {
7. super.i = j + 1;
8. System.out.println(j + " " + i);
9. }
10. }
11. class inheritance {
12. public static void main(String args[])
13. {
14. B obj = new B();
15. obj.i=1;
16. obj.j=2;
17. obj.display();
18. }
19. }
a) 2 2
b) 3 3
c) 2 3
d) 3 2
1. class A {
2. public int i;
3. private int j;
4. }
5. class B extends A {
6. void display() {
7. super.j = super.i + 1;
8. System.out.println(super.i + " " + super.j);
9. }
10. }
11. class inheritance {
12. public static void main(String args[])
13. {
14. B obj = new B();
15. obj.i=1;
16. obj.j=2;
17. obj.display();
18. }
19. }
a) 2 2
b) 3 3
c) Runtime Error
d) Compilation Error
1. class A {
2. public int i;
3. public int j;
4. A() {
5. i = 1;
6. j = 2;
7. }
8. }
9. class B extends A {
10. int a;
11. B() {
12. super();
13. }
14. }
15. class super_use {
16. public static void main(String args[])
17. {
18. B obj = new B();
19. System.out.println(obj.i + " " + obj.j)
20. }
21. }
a) 1 2
b) 2 1
c) Runtime Error
d) Compilation Error
1. class A {
2. public int i;
3. protected int j;
4. }
5. class B extends A {
6. int j;
7. void display() {
8. super.j = 3;
9. System.out.println(i + " " + j);
10. }
11. }
12. class Output {
13. public static void main(String args[])
14. {
15. B obj = new B();
16. obj.i=1;
17. obj.j=2;
18. obj.display();
19. }
20. }
a) 1 2
b) 2 1
c) 1 3
d) 3 1