0% found this document useful (0 votes)
15 views7 pages

Solutions To 2022 Java

The document contains solutions to various Java programming questions, including examples of single inheritance, string manipulation, polymorphism, and interface implementation. It also covers concepts like multithreading, applets, graphics, and file handling in Java. Each section provides code snippets and explanations for clarity.

Uploaded by

keswadmayurr
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)
15 views7 pages

Solutions To 2022 Java

The document contains solutions to various Java programming questions, including examples of single inheritance, string manipulation, polymorphism, and interface implementation. It also covers concepts like multithreading, applets, graphics, and file handling in Java. Each section provides code snippets and explanations for clarity.

Uploaded by

keswadmayurr
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
You are on page 1/ 7

Solutions to 2022 Java Programming

Questions

Q1a: Write a program to implement single inheritance in Java.

class Parent {
void display() {
System.out.println("This is the parent class.");
}
}

class Child extends Parent {


void show() {
System.out.println("This is the child class.");
}
}

public class SingleInheritance {


public static void main(String[] args) {
Child obj = new Child();
obj.display();
obj.show();
}
}

Q1b: Implement String Delete(String str, int m).

public class StringDelete {


static String Delete(String str, int m) {
if (m < 0 || m >= str.length()) {
return str; // Return the original string if the index is
invalid.
}
return str.substring(0, m) + str.substring(m + 1);
}

public static void main(String[] args) {


System.out.println(Delete("HelloWorld", 5)); // Output: Helloorld
}
}

Q1c: Explain an array, how to declare it in Java, and compare C and Java
arrays.
 Array: A data structure used to store multiple elements of the same type.
 Declaration in Java: int[] arr = new int[5];
 Comparison:
o Java arrays are objects and have built-in bounds checking, whereas C arrays are
raw memory blocks.
o Java arrays have a length property, while in C, you need to manage size
manually.

Q2a: Explain static and dynamic polymorphism with examples.

 Static Polymorphism: Method overloading.

class Polymorphism {
int add(int a, int b) {
return a + b;
}

double add(double a, double b) {


return a + b;
}
}

 Dynamic Polymorphism: Method overriding.

class Parent {
void show() {
System.out.println("Parent method.");
}
}

class Child extends Parent {


@Override
void show() {
System.out.println("Child method.");
}
}

Q2b: Write a program to join two strings.

public class JoinStrings {


public static void main(String[] args) {
String str1 = "Hello, ";
String str2 = "World!";
System.out.println(str1 + str2); // Output: Hello, World!
}
}
Q2c: Write a program to sort n numbers in an array.

import java.util.Arrays;

public class SortArray {


public static void main(String[] args) {
int[] arr = {5, 2, 8, 1, 3};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr)); // Output: [1, 2, 3, 5, 8]
}
}

Q3a: Explain the implementation of an interface with an example.

interface Animal {
void sound();
}

class Dog implements Animal {


@Override
public void sound() {
System.out.println("Bark");
}
}

public class InterfaceExample {


public static void main(String[] args) {
Animal obj = new Dog();
obj.sound(); // Output: Bark
}
}
Q3b: List advantages of packages.

1. Organization: Helps in grouping related classes.


2. Avoids Naming Conflicts: Classes with the same name can reside in different packages.
3. Access Protection: Allows the use of protected and default access specifiers.
4. Reusability: Classes from a package can be reused in multiple projects.

Q3c: List Java API packages.

Some examples:

 java.lang: Core language classes (e.g., String, Math).


 java.util: Utility classes (e.g., ArrayList, HashMap).
 java.io: Classes for input/output operations.
 java.net: Networking classes.
Q4a: Explain the concept of a package with an example.

// File: mypackage/MyClass.java
package mypackage;

public class MyClass {


public void display() {
System.out.println("This is from MyClass in mypackage.");
}
}

// File: Test.java
import mypackage.MyClass;

public class Test {


public static void main(String[] args) {
MyClass obj = new MyClass();
obj.display(); // Output: This is from MyClass in mypackage.
}
}
Q4b: Explain default interface with an example.
interface Example {
default void show() {
System.out.println("Default Method in Interface.");
}
}

class ExampleClass implements Example {}

public class DefaultInterfaceExample {


public static void main(String[] args) {
ExampleClass obj = new ExampleClass();
obj.show(); // Output: Default Method in Interface.
}
}

Q5a: What is multithreading? Explain ways to create a thread in Java.

 Multithreading: A Java feature where a program runs multiple threads simultaneously.


 Ways to Create Threads:
1. Extend Thread class.

java
Copy code
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running.");
}
}

2. Implement Runnable interface.

java
Copy code
class MyRunnable implements Runnable {
public void run() {
System.out.println("Thread is running.");
}
}

Q5b: Explain applet and differentiate between applet and application.

 Applet: A Java program that runs in a browser.


Differences:
o Applets have a lifecycle (init(), start(), stop(), destroy()), while applications
have main().
o Applets need a browser or applet viewer to run.

Q6a: Explain thread priority with an example.

class MyThread extends Thread {


public void run() {
System.out.println(Thread.currentThread().getName() + " Priority: " +
Thread.currentThread().getPriority());
}
}

public class ThreadPriorityExample {


public static void main(String[] args) {
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.start();
}
}
Q6b: Write a Java code using Buffered Reader to read a name from the user.

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ReadName {


public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter your name: ");
String name = br.readLine();
System.out.println("Hello, " + name + "!");
}
}
Q7a: Explain the Graphics class in Java. List and explain any three drawing methods.

 Graphics Class: It is part of java.awt and provides methods to draw shapes, text, and images
on components.
 Three Drawing Methods:
1. drawLine(int x1, int y1, int x2, int y2): Draws a line between two points.
2. drawRect(int x, int y, int width, int height): Draws a rectangle.
3. drawOval(int x, int y, int width, int height): Draws an oval within a
bounding rectangle.

Q7b: Create an application to display a message using Frame class.


import java.awt.*;

public class FrameExample extends Frame {


FrameExample() {
Label label = new Label("Welcome to the World of Java",
Label.CENTER);
add(label);
setSize(300, 200);
setVisible(true);
}

public static void main(String[] args) {


new FrameExample();
}
}

Q8a: Write a Java program using Swing to display "Welcome to Java."

import javax.swing.*;

public class SwingExample {


public static void main(String[] args) {
JFrame frame = new JFrame("Welcome Window");
JLabel label = new JLabel("Welcome to Java", SwingConstants.CENTER);
frame.add(label);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Q8b: Explain the hierarchy of AWT.

 AWT (Abstract Window Toolkit) hierarchy:


o java.lang.Object
 java.awt.Component
 java.awt.Container
 java.awt.Panel
 java.awt.Window
 java.awt.Frame
 java.awt.Dialog

Q8c: Write a code in Java to open a file for reading.

import java.io.*;

public class ReadFile {


public static void main(String[] args) {
try {
BufferedReader reader = new BufferedReader(new
FileReader("example.txt"));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
} catch (IOException e) {
System.out.println("File not found or unable to read.");
}
}
}

You might also like