Java Questions
Java Questions
Java Questions
a) -128 to 127
b) 0 to 256
c) 0 to 32767
d) 0 to 65535
View Answer
2. Which of these coding types is used for data type characters in Java?
a) ASCII
b) ISO-LATIN-1
c) UNICODE
d) None of the mentioned
View Answer
4. Which of these occupy first 0 to 127 in Unicode character set used for characters in Java?
a) ASCII
b) ISO-LATIN-1
c) None of the mentioned
d) ASCII and ISO-LATIN1
View Answer
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. i++;
9. }
10. }
11. }
a) i i i i i
b) 0 1 2 3 4
c) i j k l m
d) None of the mentioned
View Answer
1. class mainclass {
2. public static void main(String args[])
3. {
4. char a = 'A';
5. a++;
6. System.out.print((int)a);
7. }
8. }
a) 66
b) 67
c) 65
d) 64
View Answer
1. class mainclass {
2. public static void main(String args[])
3. {
4. boolean var1 = true;
5. boolean var2 = false;
6. if (var1)
7. System.out.println(var1);
8. else
9. System.out.println(var2);
10. }
11. }
a) 0
b) 1
c) true
d) false
View Answer
1. class booloperators {
2. public static void main(String args[])
3. {
4. boolean var1 = true;
5. boolean var2 = false;
6. System.out.println((var2 & var2));
7. }
8. }
a) 0
b) 1
c) true
d) false
View Answer
1. class asciicodes {
2. public static void main(String args[])
3. {
4. char var1 = 'A';
5. char var2 = 'a';
6. System.out.println((int)var1 + " " + (int)var2);
7. }
8. }
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
View Answer
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
View Answer
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
View Answer
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
View Answer
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
4. If an expression contains double, int, float, long, then whole expression will promoted into
which of these data types?
a) long
b) int
c) double
d) float
View Answer
1. class char_increment {
2. public static void main(String args[])
3. {
4. char c1 = 'D';
5. char c2 = 84;
6. c2++;
7. c1++;
8. System.out.println(c1 + " " + c2);
9. }
10. }
a) E U
b) U E
c) V E
d) U F
View Answer
a) 38 43
b) 39 44
c) 295 300
d) 295.04 300
View Answer
1. class A {
2. final public int calculate(int a, int b) { return 1; }
3. }
4. class B extends A {
5. public int calculate(int a, int b) { return 2; }
6. }
7. public class output {
8. public static void main(String args[])
9. {
10. B object = new B();
11. System.out.print("b is " + b.calculate(0, 1));
12. }
13. }
a) b is : 2
b) b is : 1
c) Compilation Error.
d) An exception is thrown at runtime.
View Answer
1. class main_arguments {
2. public static void main(String [] args)
3. {
4. String [][] argument = new String[2][2];
5. int x;
6. argument[0] = args;
7. x = argument[0].length;
8. for (int y = 0; y < x; y++)
9. System.out.print(" " + argument[0][y]);
10. }
11. }
a) 1 1
b) 1 0
c) 1 0 3
d) 1 2 3
View Answer
1. class c {
2. public void main( String[] args )
3. {
4. System.out.println( "Hello" + args[0] );
5. }
6. }
a) Hello c
b) Hello
c) Hello world
d) Runtime Error.
2. Which operator is used to invert all the digits in binary representation of a number?
a) ~
b) <<<
c) >>>
d) ^
View Answer
3. On applying Left shift operator, <<, on an integer bits are lost one they are shifted past
which position bit?
a) 1
b) 32
c) 33
d) 31
View Answer
1. class bitwise_operator {
2. public static void main(String args[])
3. {
4. int var1 = 42;
5. int var2 = ~var1;
6. System.out.print(var1 + " " + var2);
7. }
8. }
a) 42 42
b) 43 43
c) 42 -43
d) 42 43
View Answer
1. class bitwise_operator {
2. public static void main(String args[])
3. {
4. int a = 3;
5. int b = 6;
6. int c = a | b;
7. int d = a & b;
8. System.out.println(c + " " + d);
9. }
10. }
a) 7 2
b) 7 7
c) 7 5
d) 5 2
View Answer
1. class leftshift_operator {
2. public static void main(String args[])
3. {
4. byte x = 64;
5. int i;
6. byte y;
7. i = x << 2;
8. y = (byte) (x << 2)
9. System.out.print(i + " " + y);
10. }
11. }
a) 0 64
b) 64 0
c) 0 256
d) 256 0
View Answer
1. class rightshift_operator {
2. public static void main(String args[])
3. {
4. int x;
5. x = 10;
6. x = x >> 1;
7. System.out.println(x);
8. }
9. }
a) 10
b) 5
c) 2
d) 20
View Answer
1. class Output {
2. public static void main(String args[])
3. {
4. int a = 1;
5. int b = 2;
6. int c = 3;
7. a |= 4;
8. b >>= 1;
9. c <<= 1;
10. a ^= c;
11. System.out.println(a + " " + b + " " + c);
12. }
13. }
a) 3 1 6
b) 2 2 3
c) 2 3 4
d) 3 3 6
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
View Answer
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
View Answer
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
View Answer
a) 0
b) 1
c) Runtime error
d) classname@hashcode in hexadecimal form
2. Which of these operators can be used to concatenate two or more String objects?
a) +
b) +=
c) &
d) ||
View Answer
3. Which of these method of class String is used to obtain length of String object?
a) get()
b) Sizeof()
c) lengthof()
d) length()
View Answer
4. Which of these method of class String is used to extract a single character from a String
object?
a) CHARAT()
b) chatat()
c) charAt()
d) ChatAt()
View Answer
1. class String_demo {
2. public static void main(String args[])
3. {
4. char chars[] = {'a', 'b', 'c'};
5. String s = new String(chars);
6. System.out.println(s);
7. }
8. }
a) a
b) b
c) c
d) abc
View Answer
1. class String_demo {
2. public static void main(String args[])
3. {
4. int ascii[] = { 65, 66, 67, 68};
5. String s = new String(ascii, 1, 3);
6. System.out.println(s);
7. }
8. }
a) ABC
b) BCD
c) CDA
d) ABCD
View Answer
1. class String_demo {
2. public static void main(String args[])
3. {
4. char chars[] = {'a', 'b', 'c'};
5. String s = new String(chars);
6. String s1 = "abcd";
7. int len1 = s1.length();
8. int len2 = s.length();
9. System.out.println(len1 + " " + len2);
10. }
11. }
a) 3 0
b) 0 3
c) 3 4
d) 4 3
View Answer
1. class A {
2. int i;
3. int j;
4. A() {
5. i = 1;
6. j = 2;
7. }
8. }
9. class Output {
10. public static void main(String args[])
11. {
12. A obj1 = new A();
13. System.out.print(obj1.toString());
14. }
15. }
a) true
b) false
c) String associated with obj1
d) Compilation Error
2. Which of these can be used to fully abstract a class from its implementation?
a) Objects
b) Packages
c) Interfaces
d) None of the Mentioned.
View Answer
7. Which of the following package stores all the standard java classes?
a) lang
b) java
c) util
d) java.packages
View Answer
1. interface calculate {
2. void cal(int item);
3. }
4. class display implements calculate {
5. int x;
6. public void cal(int item) {
7. x = item * item;
8. }
9. }
10. class interfaces {
11. public static void main(String args[]) {
12. display arr = new display;
13. arr.x = 0;
14. arr.cal(2);
15. System.out.print(arr.x);
16. }
17. }
a) 0
b) 2
c) 4
d) None of the mentioned
View Answer
1. interface calculate {
2. void cal(int item);
3. }
4. class displayA implements calculate {
5. int x;
6. public void cal(int item) {
7. x = item * item;
8. }
9. }
10. class displayB implements calculate {
11. int x;
12. public void cal(int item) {
13. x = item / item;
14. }
15. }
16. class interfaces {
17. public static void main(String args[]) {
18. displayA arr1 = new displayA;
19. displayB arr2 = new displayB;
20. arr1.x = 0;
21. arr2.x = 0;
22. arr1.cal(2);
23. arr2.cal(2);
24. System.out.print(arr1.x + " " + arr2.x);
25. }
26. }
a) 0 0
b) 2 2
c) 4 1
d) 1 4
View Answer
1. interface calculate {
2. int VAR = 0;
3. void cal(int item);
4. }
5. class display implements calculate {
6. int x;
7. public void cal(int item) {
8. if (item<2)
9. x = VAR;
10. else
11. x = item * item;
12. }
13. }
14. class interfaces {
15.
16. public static void main(String args[]) {
17. display[] arr=new display[3];
18.
19. for(int i=0;i<3;i++)
20. arr[i]=new display();
21. arr[0].cal(0);
22. arr[1].cal(1);
23. arr[2].cal(2);
24. System.out.print(arr[0].x+" " + arr[1].x + " " +
arr[2].x);
25. }
26. }
a) 0 1 2
b) 0 2 4
c) 0 0 4
d) 0 1 4
2. 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
View Answer
5. Which two classes use the Shape class correctly? (Choose two.)
a) B,E
b) A,C
c) C,E
d) T,H
View Answer
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
View Answer
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
View Answer
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
View Answer
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
View Answer
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