0% found this document useful (0 votes)
16 views3 pages

Java Exam: Concepts and Programming Tasks

Uploaded by

mdfire0
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Java Exam: Concepts and Programming Tasks

Uploaded by

mdfire0
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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;
}
[Link]('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++) {
[Link](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) {
[Link](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 {
[Link](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;
[Link](x + y);

2. String str1 = 'Hello';


String str2 = 'World';
[Link](str1 + ' ' + str2);

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


for (int i = 0; i < [Link]; i++) {
[Link](array[i] + ' ');
}

4. int a = 5;
int b = 2;
double result = (double) a / b;
[Link](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.

Common questions

Powered by AI

Converting a for loop to a while loop in Java involves replicating the initialization, condition, and increment/decrement steps of the loop, which are concisely expressed in the for loop's header, into separate statements situating them appropriately around the while loop's structure. First, move the initialization statement outside before the start of the while loop. Next, transplant the conditional expression as the while loop's condition. Finally, place the increment/decrement operation at the end of the while loop's body. For instance, converting "for (int i = 0; i < 10; i++) { System.out.println(i); }" yields "int i = 0; while (i < 10) { System.out.println(i); i++; }" .

In Java, the 'final' keyword serves several important purposes: - When declared with variables (final int maxValue = 10;), the 'final' keyword ensures that the variable's value cannot be changed once it is initialized. Final variables are constants and are initialized once either at the time of declaration or in the constructor. - When declared with methods (final void display()), it prevents method overriding by subclasses, locking the method's implementation at its current state. - When declared with classes (final class MathUtility), it prevents the class from being subclassed. This is particularly useful for classes that provide global utility functions or for ensuring the immutability of complex data types .

Java's garbage collection system automatically manages memory by reclaiming memory allocated to objects that are no longer reachable in the application, thereby preventing memory leaks. The garbage collector primarily relies on determining object reachability from a set of 'root' objects; unreachable objects are then marked for garbage collection. Developers can suggest that the garbage collector run using System.gc(), though the timing of a garbage collection run is largely non-deterministic and depends on the JVM's implementation. Effective memory management strategies include minimizing object creation through object reuse, reducing the scope of variables to make objects eligible for garbage collection sooner, and avoiding memory leaks by always releasing any external resources, such as I/O streams, by using finally blocks or try-with-resources statement .

The try-catch-finally block in Java is a mechanism for handling exceptions and ensuring specific code executes regardless of whether an exception is thrown. The try block contains code that might throw an exception, the catch block contains code to handle the exception, and the finally block contains code that is executed regardless of whether an exception occurs or not. For example: ```java try { // Code that may throw an exception } catch (ExceptionType e) { // Handles exception } finally { // Cleanup code } ``` In typical use cases, try-catch-finally blocks are used to handle operations that might fail, such as file I/O operations, to ensure that resources like files and database connections are properly closed even if an exception occurs, by placing resource cleanup within the finally block .

An abstract class in Java can have both abstract methods and non-abstract methods, allowing for a partial implementation to be shared among subclasses. It can have member variables and can define constructors, providing a common state and behavior that subclasses can use. Unlike interfaces, abstract classes are declared using the abstract keyword and cannot be instantiated directly. Java interfaces, on the other hand, are used to declare methods that should be implemented by any class that claims to "implement" the interface. Interfaces can only have method signatures (with limited exceptions such as default and static methods in Java 8) and final variables; they can be used to achieve full abstraction and multiple inheritances. An interface is declared using the interface keyword. In practice, use an abstract class when there is a shared base of functionality and you expect classes to share a default implementation, whereas interfaces should be used when common behavior must be guaranteed across different classes without sharing code .

To handle an empty array without causing an error, ensure that the program logic can gracefully accommodate arrays of any size, including zero-length arrays. Since summing over an empty array results in no iterations of the sum-adding loop, ensure that the initial sum variable is initialized correctly at 0, as this will also represent the sum of an empty set correctly. Moreover, add conditional checks before operations sensitive to array element access to prevent indexing errors. For example, initially check if the array length is greater than 0 before entering loops or accessing indices to further safeguard unwarranted operations .

An ArrayList should be preferred over a LinkedList when frequent access to elements by index is needed, as ArrayLists provide O(1) time complexity for accessing elements. They are efficient when you need to perform a lot of queries for element access. Conversely, a LinkedList might be preferred when the application is heavy on insertions or deletions, especially at the beginning or end of the list, as these operations in a LinkedList can often be done in O(1) time, in contrast to the O(n) time complexity for ArrayLists. However, LinkedLists have a disadvantage of higher memory overhead due to the storage of node pointers .

The given Java program calculates and prints the sum of elements in an integer array. The code declares an integer array containing numbers from 1 to 5. A for-each loop traverses the array, adding each element to the sum variable. The final statement prints the total sum to the console. The output of the program is "Sum: 15." This result is derived by iterating over each element in the array and incrementally adding it to the sum variable initialized at 0. The additions performed are 0 + 1 + 2 + 3 + 4 + 5, resulting in the final sum of 15 .

A "for-each" loop in Java is more advantageous when simply iterating over elements of a collection or an array, where the index of the item is not required. It is particularly crisp and less error-prone for processing all elements in standard form, reducing potential off-by-one index errors and simplifying syntax. For example, iterating over an array to print its elements can benefit from a "for-each" loop since it eliminates the boilerplate code associated with handling the iteration variable and boundary conditions, ensuring smoother traversal through all elements .

When converting a do-while loop into a for loop, it is important to note that do-while loops guarantee the block of code executes at least once before the condition is tested. This inherent functionality can be represented in a for loop by appropriately setting the loop initialization, condition, increment/decrement, and ensuring the body executes the necessary initial iteration outside of the condition check or before entering a repeated evaluation state. To achieve this, initialize the loop variable before the for loop and set a high-priority internal execution flag, if necessary, to ensure the block runs unconditionally at least once, skipping an initial external condition evaluation. Applying these principles to convert "do { System.out.println(i); i++; } while (i < 10);" yields "int i = 0; for (; i < 10; i++) { System.out.println(i); }" .

You might also like