Java Fundamental Final Exam
1 mark
1. The default value of a static integer variable of a class in Java is
A. 0
B. 1
C. -1
D. Null
E. 0.0
2. Among these expressions, which is(are) of type String? 1 mark
A. “KoKo123”
B. “0123” + “23”
C. 50 + 100
D. “true”
E. ‘J’
3. Syntax that creating an array for is 1 mark
A. char oop [] = new oop[5]
B. char [] oop = new opp[5]
C. char oop [] = new char [5]
D. char [] oop = new char[5]
E. char [] oop = new char oop[5]
4. How many arithmetic operators are there? 1 mark
A. 7
B. 6
C. 5
D. 4
E. 3
5. How many primitive datatypes are there in Java? 1 mark
A. 4
B. 1
C. 8
D. 10
E. 6
1|Page
1 mark
6. Which of the following is not a valid Java identifier?
A. _myVar
B. $myVar
C. 2myVar
D. myVar2
E. #myVar
7. Which of the following is a valid declaration of a main method in a Java class? 1 mark
A. public static void main (String args[])
B. public void main (String args[])
C. static public main (String args[])
D. public static int main (String [] args)
E. public static void main (String [] args)
8. What is the size of the char data type in Java? 1 mark
A. 4 bits
B. 8 bits
C. 16 bits
D. 32 bits
E. 64 bits
1 mark
9. What is the output of the following Java code snippet?
A. 15 int a = 5;
B. 16 int b = ++a + a++ + a--;
C. 17
System.out.println(b);
D. 18
E. 19
10. What is the output of the following code snippet? 1 mark
A. 2 int a = 5;
B. 3
int b = 2;
C. 2.5
System.out.println(a / b);
D. Error
E. 1
2|Page
11. In Java, what is the purpose of the ‘break’ statement? 1 mark
A. To skip the current iteration of a loop.
B. To return a value from a method.
C. To define the endpoint of a method.
D. To exit the current loop or switch statement.
E. None of above.
12. What is printed by the following statement? 1 mark
System.out.println("Hello,\\\n\’world! ");
A. Hello,\n’world!
B. Hello,\\\n\’world!
C. Hello,\\n’world!
D. Hello,\’world!
E. Syntax Error
13. Which of the following is used to handle exceptions in Java? 1 mark
A. if-else
B. for-each
C. do-while
D. try-catch-finally
E. try-catch
14. What is the default value of a boolean variable in Java? 1 mark
A. true
B. false
C. null
D. 1
E. 0
15. What is the keyword used to define a constant variable in Java? 1 mark
A. static
B. global
C. final
D. const
E. constant
3|Page
1 mark
16. Which of the following is true?
A. A finally block is executed before the catch block but after the try block.
B. A finally block is executed, only after the catch block is executed.
C. A finally block is executed whether an exception is thrown or not.
D. A finally block is executed, only if an exception occurs.
E. None of the above.
17. What is the output of the following code snippet? 1 mark
A. Error String A = "Mg Mg";
B. 0 String B = "Mg Mg";
C. true System.out.println(A == B);
D. false
E. 1
18. What is the output of the following code snippet? 1 mark
A. false
B. true boolean result = (10 + 5) > 10 && 0 <= (5 - 5);
C. 0 System.out.println(result);
D. Error
E. Null
19. Which of the following data types is not a primitive data type in Java? 1 mark
A. int
B. float
C. boolean
D. String
E. char
20. What is the default value for an element in an integer array in Java? 1 mark
A. 0
B. -1
C. 1
D. Null
E. 0.0
4|Page
21. What is the result of System.out.println ("9" + 3 + "AB" + "2*3" + 2);
2 marks
___________________________________________
22. What is the answer of 5 * 3 + 12 / 4 - 2 according to Java internal rule of precedence? 2 marks
___________________________________________
2 marks
23. How to declare and initialize two-dimensional array in Java?
___________________________________________
2 marks
24. What will be the output of the following code?
int a = 5;
int b = 3;
int c = a++ * b--;
System.out.println(c);
____________________________________________
25. What is the value of ‘result’ in the following code? 2 marks
int x = 7;
int y = 3;
int result = x / y + x % y;
_____________________________________________
26. What is the answer of (5+1) - 5 * 7 + 9 / 7 - 2 +(-3 * 2) according to Java internal rule of
precedence? 2 marks
_____________________________________________
27. What is the result of System.out.println (20 + 3 + "CE" + 5 / 2 + "O" + 7 * 49 + "5");
2 marks
______________________________________________
5|Page
28. If d is 37, what is the value of c after the code is executed? 2 marks
int c = 12;
while (c <= d + 100) {
c = c + d;
}
_______________________________________________
29. What is the output of M in the following while loop? 2 marks
int H = 9 , M = 5 , I = 0;
while (I <= H--) {
M += I ;
I++;
}
_______________________________________________
30. Write the statement that must the loop to go through Q from 0 to 20(included) and increment
is three? 2 marks
_______________________________________________
31. What is the output of the following code? 2 marks
int[] numbers = { 0, 4, 11, 0, 44, 0, 0, 2};
for (int i = 0; i < 8; i++) {
System.out.println(numbers[i]);
__________________________________________________
32. What is the output of numbers array after the code is executed? 2 marks
int[] numbers = new int[12];
for (int i = 0; i < 12; i++) {
numbers[i] = i * i;
__________________________________________________
6|Page
33. What is the value of p after the code is executed? 2 marks
int p = 2;
for (int k = 0; k <=8; k++) {
p+=2;
}
_______________________________________________
34. What is the output of the following code? 2 marks
int value = 10;
value = 12;
if(value/2 <= 6 && value > 5) {
System.out.println("Legendary");
}
if(value != 10 || value == 12) {
System.out.println("Champion");
}
else {
System.out.println("Epic");
_______________________________________________
35. What is the output of the following code?
2 marks
int g = 14 , k = 16;
g += k;
if(g/2 <= 60 && g >= 30) {
System.out.println(g);
}
else {
System.out.println(k);
}
________________________________________________
7|Page
36. What is the value of ‘index’ after the code is executed? 2 marks
int index = 10;
while(index <= 100)
{ index+=10;
if(index == 50) break;
}
_______________________________________________
37. What is the output after the following code is executed?
2 marks
int v = 3;
v = 0;
int E = 10;
if (v != 3)
{
E *= 3;
System.out.println(E++);
if (v == 0)
{
System.out.println(++E);
System.out.println(E++);
}
}
System.out.println(++E);
___________________________________________________
38. What is the output of the following do while loop?
2 marks
String a = "Joker", a1 = "Clown";
int r = 0;
do {
a += a1;
r+=2;
} while(r < 6);
System.out.println(a);
__________________________________________________
8|Page
39. What is the output of the following code? 2 marks
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
System.out.println(cars[0]);
System.out.println(cars.length);
__________________________________________________
40. What is the output of the following nested loop?
2 marks
for (int i = 1; i <= 6; i++) {
for (int k = 1; k <= i; k++)
{
System.out.print("* ");
}
System.out.println();
}
____________________________________________________
41. What is the output of the following nested loop? 4 marks
for (int i = 1; i <= 6; i++) {
for (int j = 1; j <= (6 - i); j++)
{
System.out.print(" ");
}
for (int k = 1; k <= i; k++)
{
System.out.print(" "+i+" ");
}
System.out.println();
}
___________________________________________________
9|Page
42. A farmer is asking you to tell him how many legs can be counted among all his animals. The
farmer breads four species chickens, cows, ducks and pigs. What is the value of total number
of legs of all the animals and total of animals in the following code. 4 marks
public static void Legs(int a , int b , int c , int d) {
a = a * 2;
b = b * 4;
c = c * 2;
d = d * 4;
System.out.println("Total Legs = " + (a + b + c + d));
}
public static void main(String args[]) {
int chickens = 4, cows = 5, ducks = 6, pigs = 4;
Animals(chickens , cows , ducks , pigs);
Legs(chickens , cows , ducks , pigs);
}
public static void Animals(int a , int b , int c , int d) {
System.out.println("Chickens = " + a);
System.out.println("Cows = " + b);
System.out.println("Ducks = " + c);
System.out.println("Pigs = " + d);
}
___________________________________________________
___________________________________________________
___________________________________________________
___________________________________________________
___________________________________________________
___________________________________________________
10 | P a g e
43. It’s Birthday Party, could be your party. Look the cake how it is big and delicious. The cake
could split 15 slices. You are going to determines how that slices of cake are possible or
impossible to share your family members and friends FAIRLY if they’re 7 in the following
code. Possible or Impossible? 4 marks
public static void main(String[] args) {
int totalSlices = 15;
int numRecipients = 7;
int slicesPerPerson = 2;
if (canSplitFairly(totalSlices, numRecipients, slicesPerPerson)) {
System.out.println("It is possible to split the pie fairly.");
} else {
System.out.println("It is not possible to split the pie fairly.");
}
}
public static boolean canSplitFairly(int totalSlices, int numRecipients, int slicesPerPerson) {
return totalSlices % numRecipients == 0 && slicesPerPerson <= totalSlices /
numRecipients;
}
______________________________________________________
______________________________________________________
44. Write a Java program that output the following result of two-dimensional arrays. 4 marks
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
______________________________________________________
______________________________________________________
______________________________________________________
______________________________________________________
______________________________________________________
______________________________________________________
11 | P a g e
45. Write a program that output the following result multiply of 12. 4 marks
12 * 1 = 12
12 * 2 = 24
12 * 3 = 36
12 * 4 = 48
12 * 5 = 60
12 * 6 = 72
12 * 7 = 84
12 * 8 = 96
12 * 9 = 108
12 * 10 = 120
12 * 11 = 132
12 * 12 = 144
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
46. Write a program that output the following result. 4 marks
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
12 | P a g e
47. What is the output of the following nested loop? 8 marks
public static void main(String[] args) {
int step = 3;
int stepHeight = 3;
int stepWidth = 5;
int z = stepWidth;
for(int a = 1; a <=step; a++){
stepWidth *= a;
for(int b = 1; b <= stepWidth;b++) {
System.out.print("*");
}
System.out.println();
for(int e = 1; e<= stepHeight-2; e++) {
for(int c=1; c<= stepWidth; c++) {
if(c==1 || c==stepWidth) {
System.out.print("*");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
for(int d = 1; d <= stepWidth; d++){
System.out.print("*");
}
System.out.println();
stepWidth = z;
}
}
13 | P a g e
48. What is the output of the following nested loop? 8 marks
public static void main(String[] args) {
printDoublePyramid(7);
}
public static void printDoublePyramid(int height) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < height - i; j++) {
System.out.print(" ");
}
for (int k = 0; k <= i; k++) {
System.out.print("* ");
}
for (int j = 0; j < height - i; j++) {
System.out.print(" ");
}
for (int k = 0; k <= i; k++) {
System.out.print("* ");
}
System.out.println();
}
}
14 | P a g e
Aung Phyo Paing (Thompson LanKee)