Java Exam
Java Exam
In your analysis, discuss the purpose of each line of code and how the program works as a whole.
1. What is the output of the program? Explain how the output is derived.
2. Modify the program to calculate and print the average of the numbers in the array.
3. Explain how you can modify the program to handle an empty array without causing an error.
4. Describe how you would modify the program to find the largest number in the array.
Provide a detailed explanation of the conversion process and the resulting code.
2. Explain the difference between for loop and while loop in terms of their use cases.
3. Convert the following do-while loop into a for loop:
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 10);
4. Explain the circumstances under which you would prefer to use a for-each loop instead of a traditional for
loop.
1. int x = 5;
int y = 10;
System.out.println(x + y);
4. int a = 5;
int b = 2;
double result = (double) a / b;
System.out.println(result);
1. Write a program that calculates the factorial of a number using a for loop. Alternatively, you can use a while
loop.
2. Write a program that reverses a given string using a for loop. Alternatively, you can use a while loop.
3. Write a program that prints all prime numbers up to 100 using a for loop. Alternatively, you can use a while
loop.
4. Write a program that calculates the sum of all even numbers between 1 and 50 using a for loop. Alternatively,
you can use a while loop.
This exam covers a wide range of Java concepts and practical programming skills, ensuring a thorough assessment of
the student's knowledge and abilities.