Oops Question Bank 2024-Updated-1
Oops Question Bank 2024-Updated-1
Oops Question Bank 2024-Updated-1
6. Define Abstraction.
Abstraction in Java refers to hiding the implementation details of a code and exposing only the
necessary information to the user. It provides the ability to simplify complex systems by ignoring
irrelevant details and reducing complexity.
11. How do you declare and Initialize Array in java? Give Example.
An array is a collection of similar type of elements which has contiguous memory location.
a. Declaration of the array: Declaration of array means the specification of array variable,
data_type and array_name.
Syntax to Declare an Array in java:
dataType[] arrayRefVar; (or)
dataType []arrayRefVar; (or)
dataType arrayRefVar[];
Example: int[] floppy; (or) int []floppy (or) int floppy[];
b. Instantiation of the array: Allocating memory spaces for the declared array in memory
(RAM) is called as Instantiation of an array.
Syntax: arrayRefVar=new datatype[size];
Example: floppy=new int[10];
c. Initialization of arrays: Storing the values in the array element is called as Initialization of
arrays.
Syntax: arrayRefVar[index value]=constant or value;
Example: floppy[0]=20;
Part B
1. Discuss in detail about the basic concepts of object-oriented Programming.
2. Illustrate the Java Programming and Runtime Environment. Explain the roles of each component
of it while compiling and executing a java program.
3. a.Write a java program to receive a color code from the user (an alphabet).The program should
then print the color name, based on the color code given. The following are the color codes and their
corresponding color names. R-> Red, B->Blue, G->Green, O->Orange, Y->Yellow, W->White. If
color code provided by the user is not valid then print “Invalid Code”.
b.Write a Java code using do-while loop that counts down to 1 from 10 printing exactly ten lines
of „hello‟.
7. Create a class Rectangle which has length and breadth as data members. Define a parameterized
constructor to set the length and breadth. Write a java program to compute area and perimeter of
rectangle.
8. Write a program to initialize an integer array and find the maximum minimum value of the array.
b.Create a new class called Calculator with the following methods: A static method called
powerInt(int num1, int num2). This method should return num1 to the power num2. Another static
method called owerDouble(double num1, int num2). This method should return num1 to the power
num2. Invoke both the methods and test the functionalities. (Hint: use Math.pow(double, double)
to calculate the power.
10.Write a Java program to accept two square matrices, store them in an array, multiply the matrices
and display the result using Class and methods.
11. Display a basic calculator supporting addition, subtraction, multiplication, division, and modulus
operations. Use switch-case statements and demonstrate the use of assignment operators (=, +=, -=,
*=, /=, %=) using java program.
•Reusability of Code: Inheritance is mainly used for code reusability (Code reusability means
that we can add extra features to an existing class without modifying it).
•Effort and Time Saving: The advantage of reusability saves the programmer time and effort
since the main code written can be reused in various situations as needed.
•Increased Reliability: The program with inheritance becomes more understandable and easily
maintainable as the sub classes are created from the existing reliably working classes.
2.Tabulate the difference between the keyword “super” and “this” in java.
super this
The current instance of the parent class is The current instance of the class is
represented by the super keyword. represented by this keyword.
In order to call the default constructor of the In order to call the default constructor of the
parent class, we can use the super keyword. current class, we can use this keyword.
It can't be referred to from a static context. It It can be referred to from a static context. It
means it cannot be invoked from a static means it can be invoked from the static
context. context.
We can use it to access the data members and We can use it to access only the current class
member functions of the parent class. data members and member functions.
The Object class sits at the top (root) of the class hierarchy tree in the Java development
environment. Every class in the Java system is a descendent (direct or indirect) of the Object
class. The Object class defines the basic state and behavior that all objects must have, such as
the ability to compare oneself to another object, to convert to a string, to wait on a condition
variable, to notify other objects that a condition variable has changed, and to return the
object's class.
4.Display the syntax for importing packages in a java source file and give an example.
The import keyword is used to make the classes and interface of another package accessible
to the current package.
The Java language does not support multiple inheritances in classes because it can
result in a diamond problem, whereas there are better ways to achieve the same result than
using multiple inheritances. Consider the situation where class B extends class A and class C,
and both classes have the same display() method. It is now impossible for java compilers to
decide which display method to inherit.
To prevent such situations, Java does not allow multiple inheritances. Java has overcome
the problem of multiple inheritances by allowing classes to implement one or more interfaces.
In Java, Overriding is a feature that allows a subclass or child class to provide a specific
implementation of a method that is already provided by one of its super-classes or parent
classes. When a method in a subclass has the same name, the same parameters or signature,
and the same return type(or sub-type) as a method in its super-class, then the method in the
subclass is said to override the method in the super-class.
Classes can contain variables and methods, or functions that specify how a given data type
behaves. An interface is like a template which defines a group of related methods with empty
bodies. The implementation of the methods is provided by the class that implements the
interface.
The extends keyword is used to extend an interface, and the child interface inherits the
methods of the parent interface.
Syntax:
[accessspecifier] interface InterfaceName extends interface1, interface2,…..
{
Code for interface
}
If a class contains at least one abstract method then compulsory that we should declare the
class as abstract otherwise we will get a compile-time error, If a class contains at least one
abstract method then, implementation is not complete for that class, and hence it is not
recommended to create an object so in order to restrict object creation for such partial classes
we use abstract keyword.
PART B
1.Define thread.
A thread is a lightweight sub-process that defines a separate path of execution. It is the
smallest unit of processing that can run concurrently with the other parts (other threads) of the
same process.
2.Compare Process and Thread.
S.NO PROCESS THREAD
2) Each process has a complete set of its own Threads share the same data
variables
3) Processes must use IPC (Inter-Process Threads can directly communicate with each
Communication) to communicate with other with the help of shared variables
sibling processes
5) Process switching uses interface in Thread switching does not require calling an
operating system. operating system.
6) Processes are independent of one another Threads are dependent of one another
7) Each process has its own memory and All threads of a particular process shares the
resources common memory and resources
8) Creating & destroying processes takes Takes less overhead to create and destroy
more overhead individual threads
3.What is daemon thread and which method is used to create the daemon thread?
Daemon thread is a low priority thread that runs in background to perform tasks such as
garbage collection. Its life depend on the mercy of user threads i.e. when all the user threads
dies, JVM terminates this thread automatically.
Methods: The java.lang.Thread class provides two methods for java daemon thread.
1) public void setDaemon(boolean status) is used to mark the current thread as daemon thread
or user thread.
2) public boolean isDaemon() is used to check that current thread is daemon or not.
Exceptions can be classified into two types: There is no such classification for errors.
3. Errors are always unchecked.
a)Checked Exceptions
b)Unchecked Exceptions
In case of Checked Exceptions, compiler In case of Errors, compiler won‟t have
will have knowledge of checked exceptions knowledge of errors. Because they happen
4. and force to keep try…catch block. at run time.
Unchecked Exceptions are not known to
compiler because they occur at run time.
Exceptions are mainly caused by the Errors are mostly caused by the
5. application itself. environment in which application is
running.
Examples: Examples:
ArrayIndexOutOfBoundsException,
NullPointerException
7.What is an exception?
In Java, Exception is an unwanted or unexpected event, which occurs during the execution of
a program, i.e. at run time, that disrupts the normal flow of the program‟s instructions.
Exceptions can be caught and handled by the program. When an exception occurs within a
method, it creates an object. This object is called the exception object. It contains information
about the exception, such as the name and description of the exception and the state of the
program when the exception occurred.
String getFileName() Gets the name of the source file containing the execution
point represented by the StackTraceElement.
int getLineNumber() Gets the line number of the source file containing the
execution point represented by the StackTraceElement.
String getClassName() Gets the fully qualified name of the class containing the
execution point represented by the StackTraceElement.
String getMethodName() Gets the name of the method containing the execution
point represented by the StackTraceElement.
PART B
2. Explain how to create threads. Write a java program that prints numbers from 1 to 10 line
by line after every 5 seconds.
8. Discuss the role of the finally block in exception handling. Create a Java program that
demonstrates its use and explains how it ensures that certain code is executed regardless of
whether an exception occurs.
9. Illustrate how Java handles overflows and underflows exception with example.
10. Create your own exception for “Temperature>40” in an “Atomic Reactor Based
Application” and write appropriate exception handling code in the main program.
UNIT IV
STRING, I/O AND COLLECTION FRAMEWORK
Strings:String class, String Buffer Class-Input / Output Basics – Streams – Byte streams and
Character streams – Reading and Writing Console – Reading and Writing Files- Collection
Framework: ArrayList, Set, Map.
7.Name two classes from the java.io package that are used for byte stream operations.
In the java.io package, two commonly used classes for byte stream operations are:
FileInputStream: This class is used to read raw byte data from a file. It is a subclass of
InputStream and provides methods to read bytes from a specified file.
FileOutputStream: This class is used to write raw byte data to a file. It is a subclass of
OutputStream and provides methods to write bytes to a specified file.
boolean remove(Object key, Object value) It removes the specified values with the
associated specified keys from the map.
PART B
11.Implement the following String Buffer class methods append(), replace(), delete(),
reverse() and length() with a suitable java code.
12.Elaborate on the role of Input/Output (I/O) streams in Java. Describe the class hierarchy
of I/O streams.
13.Develop a Java program to transfer the content of one file to another file
14.Explain how to read from and write to the console in Java. Write a program that takes
multiple lines of input from the user, processes the input to count the number of words,
and outputs the result. Discuss the limitations of console-based input and how they can
be prevented.
15.Prepare a Java program that reads a text file (source.txt), searches for a specific word
(oldWord), and replaces it with another word (newWord). Save the modified content to
a new file (output.txt). Ensure efficient handling of character data and proper error
handling.
16.Compare and contrast byte streams and character streams in Java. Write a program
that reads from a text file using both byte streams and character streams, and discuss
the advantages and disadvantages of each approach.
17.Explain the ArrayList class in the Java Collection Framework with suitable example.
18.Discuss the different types of sets in the Java Collection Framework, Compare their
performance characteristics and use cases. Write a Java program that demonstrates the
use of these sets to store and manipulate a collection of unique numbers.
19.Evaluate the methods in Map with suitable java program
20.Develop a Java application for processing a large text file containing employee records.
Each record is stored as a line of text with fields separated by commas. Implement a
program that reads this text file (employee.txt), parses each record using character
streams, and computes the net_salary for all employees. Ensure efficient handling of
character data and proper error handling.
UNIT V
GENERIC PROGRAMMING AND EVENT DRIVEN PROGRAMMING
Generic Programming – Generic classes – Generic methods – Bounded Types – Restrictions
and Limitations-Basics of event handling - event handlers - adapter classes - actions - mouse
and key events –AWT - Introduction to Swing – layout management - Swing Components –
Windows–Menus– Dialog Boxes.
PART A QUESTIONS WITH ANSWERS
T show()
{
return obj;
}
void disp()
{
System.out.println(obj.getClass().getName());
}
}
Output:
java.lang.String
value : java programming with Generics
java.lang.Integer
value :550
oSet the size of the window explicitly by calling the setSize( ) method.
oMake the frame visible by calling setVisible() method.
In main() method
Create an instance of subclass.
The Font class is used to define the font style, size, and type.
The setFont() method applies the font to the specified component.
PART B
1. Discuss the following with example programs.
Generic class and Generic method
2. Explain in details about swing components
3. Implement a Java Program to display User Authentication page using AWT/Swing.
4. Explain in details about event handling?
5. Illustrate in detail about working with 2D shapes in Java.