0% found this document useful (0 votes)
16 views

Complete Java Q&A.doc

Uploaded by

hamaada2294
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Complete Java Q&A.doc

Uploaded by

hamaada2294
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

QUESTION AND ANSWER

CHAPTER ONE: JAVA OOPS CONCEPTS


1. What is Object oriented programming (OOP)?
Ans: Object-Oriented Programming is a paradigm that provides many
concepts, such as inheritance, data binding, polymorphism, etc.
Ans: Object-oriented programming is a core of Java Programming, which is
used for designing a program using classes and objects.
2. What is used OOP?
Ans: Object oriented programming is for designing a program using classes
and object.
3. How can OOPs can be characterized?
Ans: OOPs, can also be characterized as data controlling for accessing the
code.
4. What does programmers define OOPs in this approach?
Ans: In this approach, programmers define the data type of a data structure
and the operations that are applied to the data structure.
5. What is OOPs in Java?
Ans: OOps in java is to improve code readability and reusability by defining
a Java program efficiently.
6. What are the main principles of object-oriented programming?
Ans: The main principles of object-oriented programming are:
a. abstraction
b. encapsulation
c. inheritance
d. Polymorphism.
7. What are these OOPs aim to?
Ans: These concepts aim to implement real-world entities in programs.
8. List object-oriented programming system (OOPs) Concept in Java?
a. Object
b. Class
c. Inheritance
d. Polymorphism
e. Abstraction
f. Encapsulation
9. What are objects?
Ans: Objects are always called instances of a class which are created from a
class in java or any other language. They have states and behavior.
10.What are these objects correspond?
Ans: These objects always correspond to things found in the real world, i.e.,
real entities. So, they are also called run-time entities of the world.
11.What are the objects which are created from a class in java or any other
language?
Ans: the objects which are created from a class in java or any other language
are self–contained which consists of methods and properties which make
data useful.
12.What does objects can be?
Ans: Objects can be both physical and logical data. It contains addresses and
takes up some space in memory. Some examples of objects are a dog, chair,
tree etc.
13.What is the statement used for creating objects?
Ans: this is the statement used for creating objects: Mybook Myobj= new
Mybook ();
14.What is the statement used to return the value of variable (x) of an
object?
Ans: This statement is used to return the value of x of an object:
System.out.println(Myobj.x);
15.How can create multiple objects?
Ans: We can also create multiple objects in the same class and we can create
in one class and access it in another class.
Example: code
16. Mybook Myobj= new Mybook (); Where this method does is used?
Ans: This method is used for better organization of classes and always
remember that name of the java file and the class name remains the same.
17.What are classes?
Ans: Classes are like object constructors for creating objects.
Ans: The collection of objects is said to be a class.
Ans: Classes are said to be logical quantities.
18.Does classes consume any space in the memory?
Ans: Classes don’t consume any space in the memory.
19.What is called a class?
Ans: Class is also called a template of an object.
20.Does classes have members?
Ans: Classes have members which can be fields, methods and constructors.
21.What does a class has?
Ans: A class has both static and instance initializers.
22.What does a class declaration consists of?
Ans: A class declaration consists of:
a. Modifiers: These can be public or default access.
b. Class name: Initial letter.
c. Superclass: A class can only extend (subclass) one parent.
d. Interfaces: A class can implement more than one interface.
e. Body: Body surrounded by braces, { }.
23.What does a class keyword is used?
Ans: A class keyword is used to create a class.
24.What is called the variables or data defined within a class?
Ans: The variables or data defined within a class are called instance
variables. Code is always contained in the methods. Therefore, the methods
and variables defined within a class are called members of the class.
25.Does all methods have the same form?
Ans: Yes All the methods have the same form as the main () these methods
are not specified as static or public.
26.What is inheritance in java?
Ans: in java inheritance is a mechanism which one object can get all the
properties and behaviors of a parent object.
27.What does is in inheritance?
Ans: in inheritance there is a relationship between two classes that’s why it
is a relationship.
28.Give an inheritance example?
Ans: Bike, car, bus this are vehicle. That is why the child of vehicle are
bike. Car, bus, vehicle is the parent of this objects.
29.What is the important of inheritance?
Ans: the important of inheritance in java program is code reusability. We
can reuse the function of parent class at child class.
30.In java inheritance what we use?
Ans: In java inheritance we use extends keyword for inheritance.
31.What are in inheritance?
Ans: Subclass and superclass are in inheritance.
32.Write inheritance syntax?
Ans: class Parent
{
//code
}
class Child extends Parent
{
//code
}
33.Write simple inheritance program?
Ans: class super {
Public void display()
{
System.out.println(“I am parent class”);
}
}
Class sub extends super {
Public static void main(String args[])
{
Sub message = new sub()
Message.display();
}
}
34.Tell Types of inheritance?
Ans: types of inheritance are:
a. Single inheritance: is the most easy type to understand. In single
inheritance a class extend another only one class.
b. Multiple inheritance (in java Multiple inheritance is not supported)
c. Multi-level inheritance
d. Hierarchical inheritance
e. Hybrid inheritance (tan qeexitaan la’aan lee la highlight gareyay iyo
multiple inheritance-ga bo)
35.Define types of inheritance? With sample program and example?
a. Single inheritance: is the most easy type to understand.
 In single inheritance a class extend another only one class.
 The child class inherit only parent’s class.
 The subclass inherit the objects of superclass.
Example: a cat is animal. So a cat can only inherit the objects of animal
class.
Sample program of single inheritance:
class message _super {
Public void display()
{
System.out.println(“I am from superclass”);
}
}
class message_sub extends message_super {
Public static void main(String args[])
{
Message_sub message = new message_sub()
Message display();
}
}
b. When a class extends a class and that class extends another class and its
object then it call multilevel inheritance.
Example: a class A is parent class.
Class B is inherit the object of class A. another one class C is child of class
B. class C can inherit the objects of class C.
Sample program of multilevel inheritance:
class a {
int data = 15;}
class b extends a {
}
class c extends b {
Public void display() {
System.out.println(“number is:”+data);
}
Public static void main(String args[])
{
c num = new c()
num.display();
}
}
c. When one superclass can be inherited by more than one subclass then it
is called hierarchical inheritance.
Example: a class A is parent class. Class B is inherit the object of class A.
another one class c also can inherit the objects of class A.
Syntax of hierarchical inheritance:
Class food
{
//code
}
class Rice extends Food{
//code
}
class Fruit extends Food{
//code
}
36.What is inheritance method overriding? And give sample program of
method overriding with example?
Ans: when a method is already in parent class but also declaring in child
class, then it is known as Method Overriding.
Note: but when it is ‘Final’ method it cannot overriding.
Example: A parent class of animal has method ‘eat’ also a child class tiger
has method ‘eat’.
Sample program of method overriding:
class animal {
Public void display() {
System.out.println(“I am animal”);
}
class tiger extends animal {
Public void display() {
System.out.println(“I am tiger”);
}
Public static void main(String args[]){
tiiger t = new tiger()
t.display();
}
}
37. What is data Abstraction?
Ans: Data abstraction is the process of hiding certain details and showing
only essential information to the user.
38.How can Abstraction can be achieved?
Ans: Abstraction can be achieved with either abstract classes or interfaces.
39.What is the abstract keyword?
Ans: The abstract keyword is a non-access modifier, used for classes and
methods
40. Define Modifiers?
Ans: Modifiers are specific keywords present in Java using which we can
make changes to the characteristics of a variable, method, or class and limit
its scope.
41. What java programming language has?
Ans: Java programming language has a rich set of Modifiers.
42. Tell types of modifiers?
Ans: Modifiers in Java are divided into two types:
a. Access Modifiers
b. Non-Access modifiers.
43.What is the function of access modifier & Non-Access modifiers?
Ans: Access Modifiers in Java help restrict the scope of a variable, method,
class, or constructor.
Ans: Non-access modifiers provide information about the characteristics of
a class, method, or variable to the JVM.
44.What are the present modifiers in java?
Ans: Public, Private, Protected, and Default these four access modifiers
are present in Java.
45.Tell seven types of Non-Access modifier?
Ans: Seven types of Non-Access modifiers are present in Java. They are:
a. Static
b. Final
c. Abstract
d. Synchronized
e. Volatile
f. Transient
g. Native
46. Define Abstract class?
Ans: Abstract class: is a restricted class that cannot be used to create
objects (to access it, it must be inherited from another class).
47. Where an Abstract method can be used?
Ans: Abstract method: can only be used in an abstract class, and it does not
have a body.
48.What does the body provided?
Ans: The body is provided by the subclass (inherited from).
49. What method can an Abstract class have?
Ans: An abstract class can have both abstract and regular methods.
50.Why And When To Use Abstract Classes and Methods?
Ans: To achieve security - hide certain details and only show the important
details of an object.
Ans: use abstract classes and methods when you want to create a
hierarchical structure among classes.
51. What is the meaning of polymorphism? When does it occurs?
Ans: Polymorphism means "many forms", and it occurs when we have
many classes that are related to each other by in heritance.
52.How can you perform polymorphism?
Ans: You can perform Polymorphism in Java via two
different methods:
a. Method Overloading
b. Method Overriding
53. Define method overloading?
Ans: Method overloading is the process that can create multiple methods
of the same name in the same class, and all the methods work in different
ways.
54.Define method overriding?
Ans: Method overriding is the process when the subclass or a child class has
the same method as declared in the parent class.
55.When does method overloading occur?
Ans: Method overloading occurs when there is more than one method of the
same name in the class.
56. What is encapsulation?
Ans: Encapsulation: Is like a capsule the whole code and data is bounded
together into asingle unite.
Ans: Encapsulation is a process of wrapping code and data into a single unit.
Ans: Encapsulation is the way of declaring the data members as private and
providing access to the data members through public methods (getter and
setter methods).
57. As private field can’t be access outside the class what does it mean?
Ans: that means data is hiding within the class. That’s why encapsulation is
also known as data hiding. However it is possible to access them if we
provide public get and set methods.
58.What is the meaning of encapsulation?
Ans: The meaning of Encapsulation, is to make sure that "sensitive" data is
hidden from users.
59.To hide sensitive data from users what should you do?
Ans: To achieve this, you must:
a. declare class variables/attributes as private
b. provide public get and set methods to access and update the value of
a private variable
60.What is the function of get and set methods?
a. The get method returns the variable value.
b. The set method sets the value.
61.What is the syntax of Get and Set methods?
Ans: Syntax for both is that they start with either get or set followed by the
name of the variable, with the first letter in upper case.
62.Which parameter takes the set method? Where does it assign?
Ans: The set method takes a parameter (newName) and assigns it to the
name variable. This keyword is used to refer to the current object.
63. Why encapsulation is used?
Ans: Encapsulation is used for:
a. Better control of class attributes and methods
b. Class attributes can be made read-only (if you only use
the get method),or write-only (if you only use the set method)
c. Flexible: the programmer can change one part of the code without
affecting other parts
d. Increased security of data
64. Tell Advantages/Benefits of Encapsulation?
a. Improves security of an object’s internal state by hiding it from the
outside world.
b. Increases modularity and maintainability by making it easier to
change the implementation without affecting other parts of the code.
c. Enables data abstraction, allowing objects to be treated as a single
unit.
d. Allows for easy addition of new methods and fields without affecting
the existing code.
e. Supports the object-oriented principle of information hiding, making
it easier to change the implementation without affecting the rest of
the code.
65.Tell Disadvantages of Encapsulation?
a. Can lead to increased complexity, especially if not used properly.
b. Can make it more difficult to understand how the system works.
c. May limit the flexibility of the implementation.
Chapter two: Exceptional handling
66.What is the exceptional handling in java?
Ans: The Exception Handling in Java is one of the powerful mechanism to
handle the runtime errors so that the normal flow of the application can be
maintained.
67.What are exceptions in java?
Ans: In Java, Exception is an unwanted or unexpected event,
68.When exceptions does occurs?
Ans: Exceptions occurs during the execution of a program, i.e. at run time,
that disrupts the normal flow of the program’s instructions.
69.When an exception occurs within a method what happens?
Ans: when an exception occurs within a method, it creates an object.
70.What is called the object that creates when an exception occur within a
method? And what it contains?
Ans: 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.
71.What are the major reasons why an exception occurs?
Ans: Major reasons why an exception Occurs
a. Invalid user input
b. Device failure
c. Loss of network connection
d. Physical limitations (out-of-disk memory)
e. Code errors
f. Opening an unavailable file
72.What is the Difference between Error and Exception?
• Error: An Error indicates a serious problem that a reasonable
application should not try to catch.
• Exception: Exception indicates conditions that a reasonable
application might try to catch.
73.Tell Types of Exceptions?
a. Built-in Exceptions
 Checked Exception
 Unchecked Exception
b. User-Defined Exceptions
74.Define built in exceptions?
Ans: Built-in exceptions are the exceptions that are available in Java
libraries. These exceptions are suitable to explain certain error situations.
75. What is checked exceptions?
Ans: Checked Exceptions: Checked exceptions are called compile-time
exceptions because these exceptions are checked at compile-time by the
compiler.
76. Define unchecked exceptions?
Ans: Unchecked Exceptions: The unchecked exceptions are just opposite
to the checked exceptions.
77. What is error?
Ans: Error: Error is irrecoverable. Some example of errors are
OutOfMemoryError, VirtualMachineError, AssertionError etc.
78. Does the built-in exceptions in able to describe a certain situation?
Ans: Sometimes, the built-in exceptions in Java are not able to describe a
certain situation. In such cases, users can also create exceptions, which are
called ‘user-defined Exceptions’.
79.What is the function of try, catch, finally, throw and throws keywords?
a. Try: keyword is used to specify the exception block
b. Catch: keyword is used to specify the solution
c. Finally: keyword has a mandatorily executable code
d. Throw: keyword throws an exception
e. Throws: keyword is used to declare an exception
80.What does try block contains?
Ans: try block contains the code that might throw an exception. Don’t write
anything extra in try as statements after the exception will not get executed if
the exception occurred.
81.What must be followed by a try block?
Ans: Try must be immediately followed by catch or finally block.
82.What is used the catch block?
Ans: The catch block is used to catch the exception thrown by statements in
the try block. The catch must follow try else it will give a compile-time
error.
83.What are the Things to Remember?
Ans: Do not keep any code after the statement which is prone to exception.
Because if an exception occurred, it will straight away jump to the catch or
finally block, ignoring all other statements in the try block.
84.What do you need to do if you have multiple catches?
Ans: if you have multiple catches, you have to maintain the hierarchy from
subclass to superclass.

Chapter three: File Handling in Java


85.Define file hadling?
Ans: File handling in Java refers to the process of reading and writing data
from and to a file in a Java program.
Ans: File handling refers to working with the file in java.
Ans: Reading files & writing into java files is known as file handling in java.
86.What is the File?
Ans: In Java, a File is an abstract data type. A named location used to store
related information is known as a File.
Ans: The FIle is a container that can contain different types of information.
The file can contain text, images, videos, tables, etc.
87.What does the File can be?
Ans: The file can be a text file, an image file, or any other type of data that
can be stored in a file.
88.What is the goal of File handling?
Ans: The goal of file handling is to allow a program to access data stored in a
file, manipulate it, and write it back to the file.
89.In java what does the File class enables?
Ans: In java, the File class enables us to work with different types of files.
90.Define File class?
Ans: File class is a member of the java.io packages.
91.What does java provides?
Ans: Java provides various methods to read, write, update & delete files.
92.What are the File operations?
Ans: There are several File Operations like:
a. Creating a new File.
b. Getting information about File.
c. Writing into a File.
d. Reading from a File.
e. Deleting a File.
93.Before understanding file operation what is required?
Ans: Before understanding the File operations, it is required that we should
have knowledge of Stream and File methods.
94.What is a stream in java?
Ans: A stream in Java is a sequence of data. It is also defined as a sequence
of bytes.
95.What is used a stream?
Ans: It can be used to represent either an input source or a destination.
96.What is the source of destination can be?
Ans: The source and destination can be disk files, arrays, text files, etc.
97.What is the function of input/output Stream?
Ans: The input stream reads or retrieves data from a source, whereas the
output stream writes data to a destination.
98.Tell types of Stream?
Ans: There are two types of streams:
a. Byte Stream
b. Character Stream.
99.Tell in briefly I\O Streams?
Ans:
I\O Streams

Byte Stream Character Streams

Input Stream Output Stream Reader classes Writer classes


Classes Classes

100. What we use OutputStreamWriter?


Ans: We can use the OutputStreamWriter class of the byte stream to write
into a file.
101. How many bits of data this class writes at a time?
Ans: This class writes 8 bits of data at a time. We should always remember
to close the stream or else it might create dump memory.
102. What does character Stream contains?
Ans: The character stream contains the FileWriter class, which can write 16-
bits of data at a time into a file. This is a much quicker technique compared
to OutputStreamWriter as 16 bits of data is written at a time.
103. How we can read from a java file?
Ans: Similarly like write, we can read a file using byte stream and character
stream. In the byte-stream we use InputStreamreader and in the character
stream, we have FileReader to read the contents of a file.
104. What is the InputStreamReader class? And how many bits of data
can read this class at a time?
Ans: The InputStreamReader class is part of the java byte stream.
Ans: it can read 8 bits of data at a time.
105. After reading the data what happens to the file object?
Ans: After reading the data the file object should always be closed.
106. What is the FileReader class? And how mauch bits it can read?
Ans: The FileReader is a class of the character stream.
Ans: it reads 16 bits of data at a time. It is faster than InputStreamReader.
107. How a file can be deleted?
Ans: A file can be deleted using java through the method delete(). We do not
need to close the file as we do not use any reader or writer classes.
108. What the byte stream deals?
Ans: The byte stream deals with mainly byte data. We know that one byte is
equal to eight-bit. Thus, this stream mainly deals with 8bit of data. This
stream performs an Input-output operation per 8bit of data.
109. How much a Byte Stream contains?
Ans: The byte stream contains two stream classes:
a. Input Stream classes
b. Output Stream Classes.
110. What is the function of input Stream classes? And what are the
notable subclasses of the InputStream class include?
Ans: Input Stream Classes: This stream helps take input (Read Data) from
the collection in I/O File.
Ans: Some notable subclasses of the InputStream class Include:
a. AudioInputStream.
b. ByteArrayInputStream.
c. FileInputStream.
d. FilterInputStream.
e. StringBufferInputStream.
f. ObjectInputStream.
111. What is the function of Output Stream Classes? and what are the
some notable subclasses of the OutputStream class include?
Ans: Output Stream Classes: This stream helps to give output (Write Data)
into the collection in I/O File.
Ans: Some notable subclasses of the OutputStream class include:
a. ByteArrayOutputStream.
b. FIleOutputStream.
c. StringBufferOutputStream.
d. ObjectOutputStream.
e. DataOutputStream.
f. PrintStream.
112. Define the File handling process with the Character Stream? And
indicate use of character stream?
Ans: The file-handling process with a character stream is the process of
executing input data with characters.
Ans: Character Stream in Java programming is used to perform read and
write operations with character data.
113. What is the most commonly used character Stream Classes?
Ans: There are many character stream classes available, but the most
commonly used classes include:
a. FileWriter.
b. FileReader.
114. Tell the methods present in File class (java.io.File)? and their
return type and description?
No Method Return Description
Type
1. canRead() Boolean The canRead() method is used to check
whether we can read the of the file or not.
2. createNewFile() Boolean The createNewFile() method is used to create
a new empty file.
3. canWrite() Boolean The canWrite() method is used to check
whether we can write the of the file or not.
4. Exists() Boolean The exists() method is used to check whether
the specified file is present or not.
5. Delete() Boolean The delete() method is used to delete a file.
6. getName() String The getNmae() method is used to find the file
name.
7. getAbsolutePath() String The getAbsolutePath() is used to get the
absolute pathname of the file.
8. Length() Long The length() method is used to get the size of
the file in bytes.
9. List() String[] The list() method is used to get an array of the
files available in the directory.
10. Mkdir() Boolean The mkdir() method is used for creating a
new directory.
115. What is the function of these methods?
Ans: These methods can give information like name, length, path, read-only,
write-only, etc.
116. What we can use createNewFile() method? And how we recognize
that the file was creadted?
Ans: We can use the createNewFile() method to create a new file using Java.
Ans: If the method returns true, the file has been created successfully, else
the file creation was unsuccessful. That is how we can recognize.
117. Tell creating a file in java as algorithm?
Ans: Creating a File in Java:
a. Step 1: To start we need to import the necessary java classes: “java io file”
and “java io ioexception”.
b. Step 2: next we will create a public class. Here we have taken a
FileExample.
c. Step 3: Define the main method within the class.
d. Step 4: Start a try-capture block for exception management.
e. Step 5: within the try block let’s create a new file object called
“myFile.txt”.
f. Step 6: Use its “create new file” method to actually create the file.
g. Step 7: we’ll check whether this process was successful using an if else
statement.
h. Step 8: if the creation was successful we’ll print “File created” followed
by its name obtained through the file.getName() method.
i. Step 9: Otherwise if it already exists we’ll print “the file already exists”.
j. Step 10: End the if-else statement.
k. Step 11: End the try block.
l. Step 12: catch an IOexception that may occur and print an error message.
m. Step 13: print the stack trace of the exception.
n. Step 14: as a final step let’s quit out of the program.
118. What is the function of the try-catch block?
Ans: The Try-Catch block is used to handle errors if there is already a file
with the same name.

You might also like