Summer 2022 Solution _ Java
Summer 2022 Solution _ Java
z
3. Security
4. Portability
aa
Aw
5. Garbage Collection
Two key features of Java are:
ut
z
public KeywordExample(int number) {
aa
Aw
this.number = number;
}
ut
gr
z
example.printNumbers();
}
aa
Aw
}
ut
III. this: We use this keyword to refer to the current instance of the
class. In the constructor, this.number refers to the instance
variable number, and in the printNumbers() method, this is not
necessary but it demonstrates the usage.
IV. break: We use the break statement to exit the loop when the
value of i is 5.
z
Enter. aa
• Run the program by typing java Example and pressing Enter.
Aw
exception hierarchy).
Ja
Ans:
public class ExceptionHandlingExample {
public static void main(String[] args) {
try {
int[] numbers = {1, 2, 3};
int result = numbers[5] / 0; // ArithmeticException
z
System.out.println("NullPointerException: " + e.getMessage());
}
aa
Aw
catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
ut
}
gr
}
Ja
z
caught by the previous catch blocks. This is useful for handling
aa
unexpected exceptions or as a fallback option.
• By using the try-catch construct with multiple catch blocks, we
Aw
can handle different types of exceptions in a hierarchical
manner, allowing for more specific exception handling based on
the exception types.
ut
gr
Counter2(){
count++;//incrementing the value of static variable
System.out.println(count);
}
z
Counter2 c2=new Counter2(); aa
Counter2 c3=new Counter2();
Aw
}
}
ut
gr
Final Keyword:
Ja
Super keyword:
“super” is a keyword in Java that is used to refer to the superclass of
the current object. It is used to call the superclass constructor, access
the superclass’s methods, or access the superclass’s variables.
Example:
class Animal{
z
String color="white";
}
aa
Aw
class Dog extends Animal{
String color="black";
ut
void printColor(){
gr
}
}
class TestSuper1{
public static void main(String args[]){
Dog d=new Dog();
d.printColor();
}}
Ans:
In Java, when class A is the parent of class B, the following
statements can be considered:
z
i) A a = new A(); aa
This statement creates an object of class A and assigns it to a variable
Aw
z
Q.2(c) Write a java program to take infix expressions and convert it
aa
into prefix expressions.
Aw
Ans:
public class InfixToPrefixConverter {
ut
gr
z
} aa
}
Aw
if (isOperand(ch)) {
operand.append(ch);
} else if (isOperator(ch)) {
prefix.append(ch);
prefix.append(operand.reverse());
operand.setLength(0);
}
}
z
prefix.append(operand.reverse());
aa
Aw
return prefix.reverse().toString();
}
ut
gr
OR
z
} catch (NumberFormatException e) {
aa
// Expression is not a single number, evaluate it
Aw
String[] tokens = expression.split(" ");
int i = 1;
gr
Ja
switch (operator) {
case "+":
result += operand;
break;
case "-":
result -= operand;
z
}
aa
Aw
i += 2;
}
ut
return result;
gr
}
Ja
Output:
java ExpressionEvaluator "3 + 4 * 2 - 5"
Result: 6.0
z
In Java, there are five types of inheritance:
aa
1. Single Inheritance: In single inheritance, a subclass extends a
single superclass.
Aw
and so on.
gr
z
Example:
class Student3{
aa
Aw
int id;
String name;
//method to display the value of id and name
ut
z
name = n;
}
aa
Aw
//method to display the values
void display(){System.out.println(id+" "+name);}
ut
z
System.out.println(Arrays.toString(my_data) );
aa
}
Aw
}
public class Use_Demo{
ut
my_inst.display_data();
my_vals[0] = 65;
my_inst.display_data();
}
}
z
public void display_data(){
System.out.println(Arrays.toString(my_data));
aa
Aw
}
}
ut
z
import java.io.FileInputStream;
import java.io.FileOutputStream; aa
import java.io.IOException;
Aw
z
System.out.println("\nData written to the file.");
aa
} catch (IOException e) {
Aw
e.printStackTrace();
}
ut
}
gr
destination.txt.
OR
z
Q.3(a) Define types of polymorphism.
Ans: aa
In Java, polymorphism refers to the ability of an object to take
Aw
on multiple forms. There are two main types of polymorphism
supported by Java:
1. Compile-time Polymorphism (Static Polymorphism):
ut
superclass.
z
ii) Pass by Value and Pass by reference
Ans: aa
Aw
i) Arguments and Parameters of a function:
Example:
z
ii) Pass by Value and Pass by Reference: aa
Aw
Example:
void increment(int num) { // 'num' is a parameter
num++; // Increment the value of 'num'
System.out.println("Inside function: " + num);
}
int value = 5;
increment(value); // 'value' is an argument
System.out.println("Outside function: " + value);
z
Example:
class Person {
aa
Aw
String name;
ut
Person(String name) {
this.name = name;
gr
}
Ja
z
hint: use FileReader, FileWriter
Ans: aa
File IO using character streams in Java is a way of reading and writing
Aw
data to and from a file in a character-wise manner. This is useful
when we need to deal with text-based data that contains special
characters such as newline, tab, or any non-ASCII characters. Java
ut
Example:
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
z
} catch (IOException e) {
e.printStackTrace();
aa
Aw
}
}
}
ut
z
Access specifiers, also known as access modifiers, determine the
aa
level of access to the members of a class in Java. There are four
Aw
access specifiers in Java:
1. Public: Members declared as public are accessible from
anywhere in the program.
ut
z
for (int i = 0; i < 5; i++) {
aa
System.out.println("Thread is running: " + i);
Aw
}
}
}
ut
gr
z
you can define custom behavior for your threads. This allows
aa
you to perform multiple tasks concurrently or handle time-
consuming operations in the background while your main
Aw
program continues its execution.
ut
Ans:
Ja
z
objects in collections. This ensures that type errors are
aa
caught at compile-time, enhancing code reliability.
Aw
OR
z
aa
Q.4(a) Differentiate between Abstract class and Interfaces
Aw
Ans:
Abstract Class Interfaces
Defined using the abstract Defined using the interface
ut
keyword. keyword.
gr
z
Multithreading is a concept in Java that allows concurrent execution
aa
of multiple threads within a single program. The Runnable interface
is a functional interface in Java that defines a single method called
Aw
run(), which represents the code that will be executed in a separate
thread.
ut
Example:
gr
z
for (int i = 0; i < 5; i++) { aa
System.out.println("Main thread is running: " + i);
Aw
}
}
ut
}
gr
• When you run this code, you'll see that the messages from the
MyRunnable thread and the main thread are printed
interleaved, indicating concurrent execution.
z
Ans:
import java.util.ArrayList;
aa
Aw
import java.util.Collections;
import java.util.Scanner;
ut
z
• In this code, we first create an ArrayList<Integer> called numbers
aa
to store the input elements. We then use a Scanner to read the
number of elements to be entered and the actual elements from
Aw
the user.
ArrayList.
gr
When you run this program, it will prompt you to enter the number
of elements and then the elements themselves. After you've entered
all the elements, it will display the sorted output in descending order.
z
aa
ii) Throws: In Java, the throws keyword is used to declare that a
method may throw a particular type of exception. It is part of the
Aw
method signature and helps in specifying the exceptions that can be
thrown by that method. Example:
ut
divideNumbers(10, 0);
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
}
}
z
} catch (ArithmeticException e) {
aa
System.out.println("Error: " + e.getMessage());
} finally {
Aw
System.out.println("Finally block executed.");
}
}
ut
try {
return a / b;
Ja
} catch (ArithmeticException e) {
throw new ArithmeticException("Cannot divide by zero.");
}
}
}
z
start() method on the Thread instance. This method will create a new
aa
thread of execution and then call the run() method on the new
thread.
Aw
@Override
public void run() {
System.out.println("Thread is running");
}
}
z
aa
accepting user input, handling events, and organizing the layout of
the user interface.
Aw
when pressed.
2. TextField: Allows users to enter and edit text.
Ja
z
JavaFX controls are part of the JavaFX framework and are included in
aa
the Java SE platform, starting from Java 8. They offer a rich set of
features and enable developers to create modern and responsive
Aw
user interfaces for desktop, mobile, and embedded applications
OR
ut
gr
Ans:
The life cycle of a thread in Java can be described as:
z
aa
Q.5(b) In multi-threads using the Runnable interface, explain with
Aw
an example how a start() method calls the run() method of a class
implementing a runnable interface.
Ans:
ut
Example:
public class MyRunnable implements Runnable {
public void run() {
System.out.println("Thread is running");
}
z
• When you run this code, the output will be "Thread is running"
aa
because the run() method of MyRunnable is executed by the
newly created thread.
Aw
By using the start() method, you initiate the execution of the run()
method on a separate thread, allowing concurrent execution of code
ut
gr
Ja
z
@Override
aa
public void start(Stage primaryStage) {
Aw
primaryStage.setTitle("GUI Application");
// Create a label
ut
// Create a button
Ja
When you run this code, a window will appear with the label and
button. Clicking the button will update the label text accordingly.
z
aa
Aw
ut
gr
Ja