Jawaharlal Nehru Technological University Hyderabad
Jawaharlal Nehru Technological University Hyderabad B.Tech II OBJECT ORIENTED PROGRAMMING THROUGH JAVA Previous Question Paper (March - 2022 - R16)
Answer any five questions. All questions carry equal marks.
Main
What is polymorphism? Differentiate between compile time and runtime polymorphism with the help of complete java program.
Answer
Polymorphism is a fundamental concept in object-oriented programming that allows objects to be treated as instances of their parent class rather than their actual class. It enables a single interface to represent different underlying forms (data types). In Java, polymorphism is categorized into two types: compile-time (static) and runtime (dynamic).
Compile-time polymorphism is achieved through method overloading, where multiple methods in the same class share the same name but have different parameter lists. The compiler determines which method to call based on the arguments provided. Runtime polymorphism is achieved through method overriding, where a subclass provides a specific implementation of a method already defined in its superclass. The JVM determines which method to execute based on the actual object type at runtime.
class Calculator {
// Compile-time polymorphism (Method Overloading)
int add(int a, int b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
}
class Animal {
void sound() { System.out.println("Animal makes a sound"); }
}
class Dog extends Animal {
// Runtime polymorphism (Method Overriding)
@Override
void sound() { System.out.println("Dog barks"); }
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(5, 10));
System.out.println(calc.add(5, 10, 15));
Animal myAnimal = new Dog();
myAnimal.sound(); // Calls Dog's sound method
}
}Given two one dimensional arrays A and B which are sorted in ascending order. Write a program to merge them into a single sorted array C that contains every item from arrays A and B, in ascending order.
Answer
To merge two sorted arrays A and B into a single sorted array C, we use the two-pointer technique. We maintain two indices, i and j, initialized to 0, pointing to the start of arrays A and B respectively. We iterate through both arrays, comparing the elements at the current indices and appending the smaller element to array C, then incrementing the corresponding index. Once one array is exhausted, we copy the remaining elements of the other array into C.
public class MergeArrays {
public static int[] merge(int[] A, int[] B) {
int[] C = new int[A.length + B.length];
int i = 0, j = 0, k = 0;
while (i < A.length && j < B.length) {
if (A[i] <= B[j]) {
C[k++] = A[i++];
} else {
C[k++] = B[j++];
}
}
while (i < A.length) C[k++] = A[i++];
while (j < B.length) C[k++] = B[j++];
return C;
}
}The time complexity of this algorithm is O(n + m), where n and m are the lengths of arrays A and B, respectively, as each element is visited exactly once. The space complexity is O(n + m) to store the resulting merged array C.
Discuss basic in-built packages in java and their uses in application development in brief. Which package is the default package?
Answer
Sign in to view the complete answer paper.
Write a program that generates random integers and stores them in a file named “rand.dat”. The program then reads the integers from file and displays on the screen.
Answer
Sign in to view the complete answer paper.
Explain Multithreading. In how many ways java implements multithreading? Explain at least one of these ways with appropriate example.
Answer
Sign in to view the complete answer paper.
What are the differences between caught and uncaught exception? Write a program to create user defined exception.
Answer
Sign in to view the complete answer paper.
Write a java code to sort array list in descending order.
Answer
Sign in to view the complete answer paper.
Explain three UI components of swing and their classes and constructors.
Answer
Sign in to view the complete answer paper.