Java Set 1 (30 mcqs)

1. What is the range of data type short in Java?
a) -128 to 127
b) -32768 to 32767
c) -2147483648 to 2147483647
d) None of the mentioned

2. What is the range of data type byte in Java?
a) -128 to 127
b) -32768 to 32767
c) -2147483648 to 2147483647
d) None of the mentioned

3. Which of the following are legal lines of Java code?
1. int w = (int)888.8;
2. byte x = (byte)100L;
3. long y = (byte)100;
4. byte z = (byte)100L;
a) 1 and 2
b) 2 and 3
c) 3 and 4
d) All statements are correct.

4. An expression involving byte, int, and literal numbers is promoted to which of these?
a) int
b) long
c) byte
d) float

5. Which of these literals can be contained in a data type float variable?
a) 1.7e-308
b) 3.4e-038
c) 1.7e+308
d) 3.4e-050

6. Which data type value is returned by all transcendental math functions?
a) int
b) float
c) double
d) long

7. What is the output of this program?
1.class average {
2.    public static void main(String args[])
3.    {
4.        double num[] = {5.5, 10.1, 11, 12.8, 56.9, 2.5};
5.        double result;
6.        result = 0;
7.        for (int i = 0; i < 6; ++i)
8.            result = result + num[i];
9.    System.out.print(result/6);
10.
11.    }
12.}
a) 16.34
b) 16.566666644
c) 16.46666666666667
d) 16.46666666666666

8. What is the output of this program?
1.class conversion {
2.    public static void main(String args[])
3.    {
4.         double a = 295.04;
5.         int  b = 300;
6.         byte c = (byte) a;
7.         byte d = (byte) b;
8.         System.out.println(c + ” ”  + d);
9.    }
10.}
a) 38 43
b) 39 44
c) 295 300
d) 295.04 300

9. What is the output of this program?
1.class increment {
2.    public static void main(String args[])
3.    {
4.         int g = 3;
5.         System.out.print(++g * 8);
6.    }
7.}
a) 25
b) 24
c) 32
d) 33

10. What is the output of this program?
1.class area {
2.    public static void main(String args[])
3.    {
4.         double r, pi, a;
5.         r = 9.8;
6.         pi = 3.14;
7.         a = pi * r * r;
8.         System.out.println(a);
9.    }
10.}
a) 301.5656
b) 301
c) 301.56
d) 301.56560000

11. Which of these operators is used to allocate memory to array variable in Java?
a) malloc
b) alloc
c) new
d) new malloc

12. Which of these is an incorrect array declaration?
a) int arr[] = new int[5]
b) int [] arr = new int[5]
c) int arr[]
arr = new int[5]
d) int arr[] = int [5] new

13. What will this code print?
int arr[] = new int [5];
System.out.print(arr);
a) 0
b) value stored in arr[0].
c) 00000
d) Garbage value

14. Which of these is an incorrect Statement?
a) It is necessary to use new operator to initialize an array.
b) Array can be initialized using comma separated expressions surrounded by curly braces.
c) Array can be initialized when they are declared.
d) None of the mentioned

by curly braces example : int arr[5] = new int[5]; and int arr[] = { 0, 1, 2, 3, 4};
15. Which of these is necessary to specify at time of array initialization?
a) Row
b) Column
c) Both Row and Column
d) None of the mentioned

16. What is the output of this program?
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

17. What is the output of this program?
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

10
18. What is the output of this program?
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

19. What is the output of this program?
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

20. What is the output of this program?
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

21. Which of these operators is used to allocate memory to array variable in Java?
a) malloc
b) alloc
c) new
d) new malloc
.
22. Which of these is an incorrect array declaration?
a) int arr[] = new int[5]
b) int [] arr = new int[5]
c) int arr[]
arr = new int[5]
d) int arr[] = int [5] new

23. What will this code print?
int arr[] = new int [5];
System.out.print(arr);
a) 0
b) value stored in arr[0].
c) 00000
d) Garbage value

24. Which of these is an incorrect Statement?
a) It is necessary to use new operator to initialize an array.
b) Array can be initialized using comma separated expressions surrounded by curly braces.
c) Array can be initialized when they are declared.
d) None of the mentioned

25. Which of these is necessary to specify at time of array initialization?
a) Row
b) Column
c) Both Row and Column
d) None of the mentioned

26. What is the output of this program?
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

27. What is the output of this program?
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

28. What is the output of this program?
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

29. What is the output of this program?
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

30. What is the output of this program?
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

Answers

1-b 2-a 3-d 4-a 5-b
6-c 7-c 8-b 9-c 10-a
11-c 12-d 13-d 14-a 15-a
16-a 17-b 18-d 19-d 20-b
21-c 22-d 23-d 24-a 25-a
26-a 27-b 28-d 29-d 30-b

Sharing is caring!

Leave a Comment

Your email address will not be published. Required fields are marked *

shares