Java 2022 (Repeater) Solved Question Paper
Section - A
Answer All The Questions (10*2=20)
1. Write All Answers
a) What is encapsulation ?
Encapsulation is a technique by which multiple related objects can be grouped under one object. Java implements encapsulation by the use of Packages.
b) What is garbage collection ?
- Garbage Collection is process of reclaiming the runtime unused memory automatically. In other words, it is a way to destroy the unused objects.
- In java, garbage means unreferenced objects.
c) Define vector List any two methods.
In Java, a vector is a dynamic array that can grow or shrink its size as needed. It is a type of data structure that can store a collection of objects in a sequential order.
- add() – This method adds a new element to the end of the vector.
- get() – This method returns the element at the specified index in the vector.
d) What is Constructor ?
- Constructor in java is a special type of method that is used to initialize the object.
- Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor.
e) Define wrapper class.
A wrapper class in Java is a class that wraps around a primitive data type and converts it into an object. This means that the primitive data type can be used in places where only objects are allowed, such as in collections and generic methods.
f) What are access modifiers in java ?
Access modifiers in Java are keywords that are used to control the visibility of classes, fields, methods, and constructors. There are four access modifiers in Java:
- Public
- Private
- Default
- Protected
g) Define exception handling?
- Handling run time errors is called exception handling.
- The exception handling in java is one of the powerful mechanism to handle the runtime errors so that
normal flow of the application can be maintained.
h) What is applet?
- Applets are small java programs that are primarily used in Internet computing.
- They can be transported over the Internet from one computer to another and run using Applet Viewer or any web browser that supports java.
i) State methods of Graphic class.
- getColor() – Returns the current color of the graphics context.
- setFont() – Sets the current font of the graphics context.
- getFont() – Returns the current font of the graphics context.
- getLineWidth() – Returns the current line width of the graphics context.
j) Explain “public static void main (String args[ ] )”
The “public static void main (String args[ ] )” method is the entry point for Java programs. It is the first method that is called when a Java program is executed.
The (public) keyword means that the method is accessible from anywhere in the program.
The (static) keyword means that the method can be called without having to create an instance of the class in which it is declared.
The (void) keyword means that the method does not return a value.
The (args) parameter is an array of strings that contains the command-line arguments that were passed to the program when it was executed.
Section - B
Answer any Four of the following. (4*5=20)
2. Explain java features.
Java Features:
- Compiled and Interpreted – Basically a computer language is either compiled or interpreted. Java comes together both these approach thus making Java a two-stage system. Java compiler translates Java code to Byte code instructions and Java Interpreter generate machine code that can be directly executed by machine that is running the Java program.
- Platform Independent and portable – Java supports the feature portability. Java programs can be easily moved from one computer system to another and anywhere. Changes and upgrades in operating systems. Java certifies portability in two ways. First way is, Java compiler generates the byte code and that can be executed on any machine. Second way is, size of primitive data types are machine independent.
- Object- oriented – Java is truly object-oriented language. In Java, almost everything is an Object. All program code and data exist in objects and classes. Java comes with an extensive set of classes; organize in packages that can be used in program by Inheritance. The object model in Java is trouble-free and easy to enlarge.
- Robust and secure – Java is a most strong language which provides many securities to make
certain reliable code. - Distributed – Java is called as Distributed language for construct applications on networks which can contribute both data and programs. Java applications can open and access remote objects on Internet easily. That means multiple programmers at multiple remote locations to work together on single task.
- Simple and small – Java is very small and simple language. Java does not use pointer and header files, goto statements, etc. It eliminates operator overloading and multiple inheritance.
- Multithreaded – and Interactive Multithreaded means managing multiple tasks simultaneously. Java maintains multithreaded programs. That means we need not wait for the application to complete one task before starting next task. This feature is helpful for graphic applications.
- High performance – Java performance is very extraordinary for an interpreted language, majorly due to the use of intermediate bytecode. Java architecture is also designed to reduce overheads during runtime. The incorporation of multithreading improves the execution speed of program.
- Dynamic and Extensible – Java is also dynamic language. Java is capable of dynamically linking in new class, libraries, methods and objects. Java can also establish the type of class through the query building it possible to either dynamically link or abort the program, depending on the reply.
3. Explain Thread priority.
THREAD PRIORITY
- For Executing a Program java Contains a Scheduler which Executes the Programs of java on the
behalf of Priorities and because a Process or can Execute only one Thread at a time and the Priority of a
Thread will determine which Thread will be Executed now and by default all the Threads have a Same
Priorities. But if a Thread has a Higher Priority then this will be Executed First and then after other lower
Priorities Thread will be Executed. Generally JAVA Provided us three types of Priorities those are MIN_
Priority , Max-Priority and Normal_ Priority They are used as.- MAX_PRIORITY
- NORM_PRIORITY
- MAX_PRIORITY
In this Minimum Priority of a thread has value 0 and Normal Priority has value 5 and Maximum
Priority has a value 10.
class A extends Thread
{
public void run()
{
System.out.println("Class A starts");
for(int i=1;i<=5;i++)
{
System.out.println("i = "+i);
}
System.out.println("Class A exits");
}
}
class B extends Thread
{
public void run()
{
System.out.println("Class B starts");
for(int j=1;j<=5;j++)
{
System.out.println("j = "+j);
}
System.out.println("Class Bexits");
}
}
class C extends Thread
{
public void run()
{
System.out.println("Class C starts");
for(int k=1;k<=5;k++)
{
System.out.println("k = "+k);
}
System.out.println("Class C exits");
}
}
class PriorityDemo
{
public static void main(String args[])
{
A a=new A();
B b=new B();
C c=new C();
c.setPriority(Thread.MIN_PRIORITY);
a.setPriority(Thread.MAX_PRIORITY);
b.setPriority(Thread.NORM_PRIORITY);
c.start();
a.start();
b.start();
}
}
4. Write a procedure to create packages.
- Choose a name for your package. The package name should be unique and descriptive. It should also follow the Java naming conventions, which means that it should be all lowercase and the words should be separated by dots.
- Create a directory for your package. The directory name should be the same as the package name.
- Create a Java file in the directory and put the following line at the top of the file:
package <package_name>;
This line tells the Java compiler that the class in the file belongs to the specified package.
4. Define your class in the file.
For example, the following code shows how to create a package called
my_package
and a class calledMyClass
inside the package:package my_package;
{
public class MyClass
{
public static void main(String[] args
{
System.out.println("Hello, world!");
}
}Once you have created the package and the class, you can compile the code using the following command:
javac MyClass.java
This will create a bytecode file called
MyClass.class
. You can then run the program using the following command:java my_package.MyClass
This will print “Hello, world!” to the console.
You can also create subpackages within a package. To do this, simply create a new directory inside the package directory and name it the same as the subpackage name. Then, create a Java file in the subpackage directory and put the following line at the top of the file:
Javapackage <package_name>.<subpackage_name>;
This line tells the Java compiler that the class in the file belongs to the specified subpackage.For example, the following code shows how to create a subpackage called
my_subpackage
inside themy_package
package and a class calledMySubClass
inside the subpackage:Javapackage my_package.my_subpackage; public class MySubClass
{ public static void main(String[] args)
{ System.out.println("Hello from the subpackage!"); } }Once you have created the subpackage and the class, you can compile and run the code in the same way as you did for theMyClass
class.Packages are a powerful tool for organizing and managing Java code. They can help to make your code more modular and reusable. They can also help to prevent name conflicts between different classes.
5. Write java program to demonstrate at least 5 string methods using scanner class.
import java.util.*;
public class StringMethodsDemo {
public static void main(String[] args) {
// Create a Scanner object to read input from the console.
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter a string.
System.out.println("Enter a string: ");
// Read the string from the console.
String inputString = scanner.nextLine();
// Demonstrate the following string methods:
// 1. length()
int stringLength = inputString.length();
System.out.println("The length of the string is: " + stringLength);
// 2. charAt()
char firstCharacter = inputString.charAt(0);
System.out.println("The first character of the string is: " + firstCharacter);
// 3. substring()
String substring = inputString.substring(1, 5);
System.out.println("The substring from index 1 to index 5 is: " + substring);
// 4. toLowerCase()
String lowercaseString = inputString.toLowerCase();
System.out.println("The lowercase version of the string is: " + lowercaseString);
// 5. toUpperCase()
String uppercaseString = inputString.toUpperCase();
System.out.println("The uppercase version of the string is: " + uppercaseString);
// Close the Scanner object.
scanner.close();
}
}
6. Explain applet life cycle with neat diagram.
1) Initialization State – when the browser downloads an HTML page containing applets, it creates an instance for each of the
Applet classes, using the no arg constructor. Applet must have a no argument constructor otherwise it
cannot be loaded. Initialization can be done through init(). The init() method is the first method to be
called. It is used to initialize the applet each time it is reloaded. Applets can be used for setting up an
initial state, loading images or fonts, or setting parameters. For example:
public void init()
{
//code here
}
2) Running State – Immediately after calling init(), the browser calls the start() method. start() is also called when user
returns to an HTML page that contains the applet. So, it ht user leaves a web page and come back, the
applet resumes execution at start(). So, when the applet called the start() Method it is called its running
state. Staring can also occur if the applet is already in “stopped” (idle) state.
public void start()
{
(Action)
}
3) Idle or Stopped State – When we leave the page containing the currently running applet, then it stop running and becomes idle.
We can also do so by calling stop() Method explicitly.
public void stop()
{
(Action)
}
4) Dead State – When an applet is completely removed from the Memory, it is called dead. This occurs automatically by
invoking the destroy() method when we quit the browser Like initialization, dead state occurs only once
in the applet’s life cycle.
public void destroy()
{
(Action)
}
5) Display State – Painting is how an applet displays something on screen-be it text, a line, a colored background, or an
image. The paint() method is used for displaying anything on the applet paint() method takes an
argument, an instance of class graphics. The code given can be as follows:
public void paint(Graphics g)
{
//Code
}
where Graphics class contains the member functions that can be used to display the output to the browser.
Section - C
Answer any Four of the following. (4*10=40)
7. Two Que (a & b)
a) Explain method overriding with example.
- If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in java.
- In other words, If subclass provides the specific implementation of the method that has been provided by
one of its parent class, it is known as method overriding.
Usage of Java Method Overriding
- Method overriding is used to provide specific implementation of a method that is already
provided by its super class. - Method overriding is used for runtime polymorphism
Rules for Java Method Overriding
- method must have same name as in the parent class
- method must have same parameter as in the parent class.
- must be IS-A relationship (inheritance).
Advantage of method overriding
- The main advantage of method overriding is that the class can give its own specific implementation to a
inherited method without even modifying the parent class(base class). - Example program of Method Overriding
class Animal
{
void eat()
{
System.out.println("Animal is eating");
}
}
class Dog extends Animal
{
void eat()
{
System.out.println("Dog is eating");
}
}
class Overriding
{
public static void main(String args[])
{
Dog d=new Dog();
d.eat();
}
}
b) Difference between C++ & java.
- First Release In October 1985.
- It is not portable.
- Platform dependent
- C++ is a Compiled Language.
- Memory Management in C++ is Manual.
- It has Virtual keywords.
- It supports both single and multiple Inheritance.
- It strongly supports pointers.
- C++ supports Structures and Unions.
- C++ supports goto keyword.
- C++ is both a procedural and an object-oriented programming language.
- First Release on May 23, 1995.
- It is portable.
- Platform-independent.
- Java is both Compiled and Interpreted Language.
- Memory Management is System Controlled.
- It doesn’t have a Virtual Keyword.
- It supports only single inheritance. Multiple inheritances are achieved partially using interfaces.
- It has limited support for pointers.
- Java doesn’t support Structures and Unions.
- Java doesn’t support goto Keyword.
- Java is only an object-oriented programming language.
8. Write a short note on
a) Final Keyword.
The final keyword in Java is used to declare constants, prevent inheritance, and prevent methods from being overridden.
Constants: A constant is a variable whose value cannot be changed once it has been initialized. To declare a constant, you use the final
keyword before the variable type. For example:
final int MY_CONSTANT = 10;
final
keyword to prevent a class from being inherited. This can be useful if you want to prevent other classes from changing the behavior of your class. For example:final class MyClass
{
// ...
}
final
keyword to prevent a method from being overridden. This can be useful if you want to ensure that a method is always implemented in the same way. For example:final class MyClass
{
final void myMethod()
{
// ...
}
}
b) Static Method
If you apply static keyword with any method, it is known as static method.
• A static method belongs to the class rather than object of a class.
• A static method can be invoked without the need for creating an instance of a class.
• static method can access static data member and can change the value of it.
Example of static method
class Student
{
int rollno;
String name;
static String college = "ITS";
static void change()
{
college = "BCA";
}
Student(int r, String n)
{
rollno = r;
name = n;
}
void display ()
{
System.out.println(rollno+" "+name+" "+college);
}
}
class StaticMethod
{
public static void main(String args[])
{
Student9.change();
Student9 s1 = new Student9 (111,"Karan");
Student9 s2 = new Student9 (222,"Aryan");
Student9 s3 = new Student9 (333,"Sonoo");
s1.display();
s2.display();
s3.display();
}
}
c) wait( ) and Sleep ( )
The wait() and sleep() methods in Java are both used to pause the execution of a thread. However, there are some key differences between the two methods.
- wait()
The wait() method is used to pause the execution of a thread until it is notified by another thread. The wait() method is typically used in conjunction with the notify() and notifyAll() methods.
To use the wait() method, you must first acquire the lock on the object that the thread is waiting on. You can do this using the synchronized()
block statement. Once you have acquired the lock, you can call the wait() method. The wait() method will release the lock and put the thread into a waiting state.
- sleep()
The sleep() method is used to pause the execution of a thread for a specified period of time. The sleep() method does not release the lock on the object that the thread is holding.
To use the sleep() method, you simply pass the amount of time in milliseconds that you want the thread to sleep for. For example, the following code will cause the thread to sleep for 1 second:
9. Explain Oops Concepts.
OOP concepts in Java:
- Classes and Objects: In Java, a class is a blueprint or template for creating objects. An object is an instance of a class that contains data (fields) and behavior (methods). Classes define the properties and behavior of objects, and objects are created from classes using the “new” keyword.
- Encapsulation: Encapsulation is the process of hiding the internal details of an object from the outside world and providing a public interface for accessing and manipulating the object’s state. In Java, encapsulation is achieved through the use of access modifiers, such as public, private, and protected.
- Inheritance: Inheritance is the process by which one class can inherit the properties and behavior of another class. In Java, inheritance is achieved using the “extends” keyword, where a subclass (the class that inherits) extends a superclass (the class being inherited from).
- Polymorphism: Polymorphism is the ability of objects of different classes to be treated as if they are of the same type. In Java, polymorphism is achieved through method overriding and method overloading. Method overriding is when a subclass provides its own implementation of a method that is already defined in the superclass. Method overloading is when a class provides multiple methods with the same name but different parameter lists.
- Abstraction: Abstraction is the process of creating a simplified view of an object or system by hiding its complex details. In Java, abstraction is achieved through the use of abstract classes and interfaces. An abstract class is a class that cannot be instantiated and is used as a base class for other classes. An interface is a collection of abstract methods that define a set of behaviors that a class must implement.
10. Define Interface . Write program to demonstrate multiple inheritance.
An interface is like a class but includes a group of methods declarations.
public interface Animal
{
void eat();
}
public interface Bird extends Animal
{
void fly();
}
public class Parrot implements Bird
{
@Override
public void eat()
{
System.out.println("The parrot is eating.");
}
@Override
public void fly()
{
System.out.println("The parrot is flying.");
}
}
public class Main
{
public static void main(String[] args)
{
Parrot parrot = new Parrot();
parrot.eat();
parrot.fly();
}
}
11. Write java program to demonstrate method over loading?
class Geometry
{
double width,height; int
radius;
void area(double x,double y)
{
width=x;
height=y;
double area=width*height; System.out.println("Area
of Rectangle is = "+area);
}
void area(int x)
{
radius=x;
double area=3.142*radius*radius;
System.out.println("Area of Circle is = "+area);
}
}
class Overloading
{
public static void main(String args[])
{
Geometry g=new Geometry();
g.area(10.2,15.3);
g.area(5);
}
}