Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
49 views

Java Exam

Uploaded by

mdfire0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Java Exam

Uploaded by

mdfire0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Exam

Question 1: Choose the correct answer


1. Which of the following is a valid declaration of an array in Java?
A) int[] arr = new int[5];
B) int arr = new int[5];
C) int arr[] = new int(5);
D) int[] arr = new int(5);
2. Which of these access specifiers can be used for a class so that it is accessible only within the same package?
A) public
B) protected
C) private
D) default
3. Which method must be implemented by all threads in Java?
A) start()
B) run()
C) stop()
D) main()
4. What is the size of an int variable in Java?
A) 4 bytes
B) 8 bytes
C) 16 bytes
D) Depends on the system

Question 2: Choose one question


1. Explain the difference between abstract class and interface in Java. Provide examples to illustrate your
points.
2. Describe how garbage collection works in Java. Include information on how you can suggest that the garbage
collector run and how you can manage memory more effectively.
3. What is method overloading in Java? Provide an example to demonstrate method overloading.
4. Explain the significance of the final keyword in Java. Provide examples of its use with variables, methods, and
classes.

Question 3: Choose one question


1. What is the difference between ArrayList and LinkedList in Java? Explain the scenarios where you would
prefer one over the other.
2. Explain the concept of multithreading in Java and how it is achieved. Include an example of creating and
running a thread.
3. Discuss the difference between HashMap and TreeMap in Java. Provide examples of when to use each.
4. Explain how the try-catch-finally block works in Java. Provide an example demonstrating its use.

Question 4: Analyze a Java program


Analyze the following Java program and explain what it does:
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
int sum = 0;
for (int number : numbers) {
sum += number;
}
System.out.println('Sum: ' + sum);
}
}

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.

Question 5: Convert from loop to loop


Convert the following for loop into a while loop:
for (int i = 0; i < 10; i++) {
System.out.println(i);
}

Provide a detailed explanation of the conversion process and the resulting code.

1. Convert the following while loop into a for loop:


int i = 0;
while (i < 10) {
System.out.println(i);
i++;
}

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.

Question 6: Give the outputs


What will be the output of the following code snippets? Explain each output.

1. int x = 5;
int y = 10;
System.out.println(x + y);

2. String str1 = 'Hello';


String str2 = 'World';
System.out.println(str1 + ' ' + str2);

3. int[] array = {1, 2, 3, 4, 5};


for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + ' ');
}

4. int a = 5;
int b = 2;
double result = (double) a / b;
System.out.println(result);

Provide a detailed explanation of why each output is produced as it is.


Question 7: Write a program using loops and choose one from two loops
Write a Java program that prints the first 10 numbers of the Fibonacci sequence using a for loop. Alternatively, you
can use a while loop. Provide detailed comments explaining each part of your code.

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.

Question 8: Write a program (functions and a single matrix) for 100


students
Write a Java program that initializes a 2D array to store the marks of 100 students in 5 subjects. Use functions to:

1. Calculate and display the average marks of each student.


2. Calculate and display the highest and lowest marks in each subject.
3. Calculate and display the overall average marks for all students.
4. Calculate and display the total marks obtained by each student.

Question 9: Write a program (functions) and a 1D matrix


Write a Java program that initializes a 1D array with 10 elements. Use functions to:

1. Find and display the maximum value in the array.


2. Find and display the minimum value in the array.
3. Calculate and display the average value of the elements in the array.
4. Sort the array in ascending order and display the sorted array.

Question 10: Write a program (functions and a 2D matrix)


Write a Java program that initializes a 2D matrix and transposes it. Use functions to:

1. Input the matrix.


2. Transpose the matrix.
3. Output the original and transposed matrices.
4. Calculate and display the sum of all elements in the original matrix.

This exam covers a wide range of Java concepts and practical programming skills, ensuring a thorough assessment of
the student's knowledge and abilities.

You might also like