0% found this document useful (0 votes)
18 views14 pages

Java Theory Question Answer

The document discusses fundamental concepts of Object-Oriented Programming (OOP) and Java features, including procedural vs. object-oriented programming, encapsulation, polymorphism, inheritance, and Java's characteristics such as portability and security. It also covers Java's data types, control structures, constructors, method overloading, and multithreading. Additionally, it explains key programming concepts like tokens, type casting, and the differences between classes and interfaces.
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)
18 views14 pages

Java Theory Question Answer

The document discusses fundamental concepts of Object-Oriented Programming (OOP) and Java features, including procedural vs. object-oriented programming, encapsulation, polymorphism, inheritance, and Java's characteristics such as portability and security. It also covers Java's data types, control structures, constructors, method overloading, and multithreading. Additionally, it explains key programming concepts like tokens, type casting, and the differences between classes and interfaces.
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/ 14

Basic Concepts of OOP & Java features

1. What is Procedure oriented programming approach. Write its


characteristics.
The characteristics of procedural programming are:
1. Procedural programming follows a top-down approach.
2. The program is divided into blocks of codes called functions, where each
function performs a specific task.
3. Procedural programs model real-world processes as 'procedures'
operating on 'data'.
4. The data and functions are detached from each other.
5. The data moves freely in a program.
6. It is easy to follow the logic of a program.
7. A function can access other function's data by calling that function.

2. What is object-oriented programming approach. Write its characteristics.


Object-oriented programming (OOP) is a computer programming model that
organizes software design around data, or objects, rather than functions and
logic..
Object-oriented methodology is a way of viewing software components and their
relationships. Object-oriented methodology relies on three characteristics that
define object-oriented languages: encapsulation, polymorphism, and inheritance.
These three terms are elaborated below.
3. Differentiate between procedure oriented and object-oriented
programming approaches.
[Notes]
4. Write the basic concepts of object-oriented programming paradigm.
[notes]
5. Differentiate between class and objects.
notes
6. What is data encapsulation.
notes
7. What is data hiding.

Data hiding is a technique of hiding internal object details, i.e., data


members. It is an object-oriented programming technique. Data hiding
ensures, or we can say guarantees to restrict the data access to class
members. It maintains data integrity.
8. What is data abstraction. How it is different from data encapsulation.

Data Abstraction can be described as the Data Encapsulation can be described as


technique of hiding internal details of a the technique of binding up of data along
program and exposing the functionality with its correlate methods as a single
only. unit.
Implementation hiding is done using this Information hiding is done using this
technique. technique.
Data abstraction can be performed using Data encapsulation can be performed
Interface and an abstract class. using the different access modifiers like
protected, private, and packages.
Abstraction lets somebody see Encapsulation conceals or covers
the What part of program purpose. the How part of the program's purpose.

9. What is compile time polymorphism.


Compile time polymorphism in Java is also known as early binding or static
polymorphism. The binding is performed during compilation time and hence
the name compile-time polymorphism. Binding refers to connecting the
function call to the function body.
10. What is run time polymorphism.
Runtime polymorphism in Java is also popularly known as Dynamic Binding or
Dynamic Method Dispatch. In this process, the call to an overridden method is
resolved dynamically at runtime rather than at compile-time. You can achieve
Runtime polymorphism via Method Overriding.
11. Differentiate between static binding and dynamic binding.
Sr. Key Static Binding Dynamic Binding
No.

1 Basic It is resolved at compile time It is resolved at run


time

2 Resolve static binding use type of the Dynamic binding uses


mechanism class and fields object to resolve
binding

3 Example Overloading is an example of Method overriding is


static binding the example of
Dynamic binding

4. Type of private, final and static methods Virtual methods use


Methods and variables uses static binding dynamic binding

12. How are data and methods organized in an object-oriented program.


In an object-oriented program, the data and methods are
organized with the help of classes. A class has the data members
or attributes and the member function that manipulates the data
members or accomplishes the class features and functionalities.
13. Write some of the benefits of OOP.
CODE ORGANIZATION:
One of the main benefits of OOP as it relates to Java is that it gives
developers the ability to encapsulate data and behaviour within
classes and objects, keeping a focus on data integrity and preventing
accidental access to information.
CODE REUSABILITY:
There are several OOP mechanisms Java provides to promote code
reusability. One of those is inheritance.
14. What is inheritance. Write the advantages of inheritance.
Inheritance helps in code reuse. The child class may use the code
defined in the parent class without re-writing it. Inheritance can
save time and effort as the main code need not be written again.
Inheritance provides a clear model structure which is easy to
understand.
15. List the different features of java language.
Major Features of Java Programming Language
• Simple.
• Object-Oriented.
• Platform Independent.
• Portable.
• Robust.
• Secure.
• Interpreted.
• Multi-Threaded.

16. Java is known as a compiled and interpreted language. Justify.


Java programming language uses both the compiler and an interpreter because the
source programming code is first transformed into binary programming code. And,
this code is run by the JVM, which is usually a software-based interpreter.
By using compiled byte code, the interpreter can be small and useful.

17. Java ensures portability. Justify.


Java programs are portable, which means that the same bytecode program can run on any
computer system that has a Java interpreter. Also, a source program can be compiled into
bytecodes on any computer that has a Java compiler.

18. Why java is known as a platform independent language.


• Java provides Platform Independence by making use of Java Byte Code.
Java Byte Code or .class file is generated during the compilation of the
code. This Byte Code is platform-independent and can run on any system
regardless of the platform it is built upon.
• Byte Code differs from machine code which cannot be transferred to
another platform. And Java Virtual Machine, an Interpreter, can run the
Byte Code.

19. How is java more secured than other languages.


It does not use explicit pointer. So, Java does not allow access of memory area
directly which makes it more secure and keeps it safe from many sources of bugs.
Class Loader only loads the proper class to be executed and the byte code verifier
checks the code fragments which makes the execution of classes secure.

20. What is bytecode in java.


Bytecode in Java is a set of instructions for the Java Virtual Machine. Bytecode
is a platform-independent code. Bytecode is a code that lies between low-level
language and high-level language. After the Java code is compiled, the
bytecode gets generated, which can be executed on any machine using JVM.

Operator, Conditional and Looping, array and string


1. What is a token. List the various types of tokens supported by java.
In Java, tokens are the basic building blocks of a program.
There are five types of java tokens: keywords, identifiers, literals,
operators and separators.
2. Java is a freeform language. Comment.
Java is a free form language because whitespace is used only to denote tokens,
and otherwise ignored by the compiler and thus the JVM.
In Java all the codes from top to bottom can be written without pressing
‘enter’, and leaving ‘space’ between codes definition, in one single line. So,
whole Java could be written in one single long sentence. So, it’s a free form
language that means you have an option to use space and use indentation only
if you want your code to be written in the way that is readable to yourself later
on or to other people around you.
3. What is the task of main method in java.
The Java main method is usually the first method you learn about when you
start programming in Java because its the entry point for executing a Java
program. The main method can contain code to execute or call other methods,
and it can be placed in any class that's part of a program.
4. List the different basic data types used in java.
Primitive data types - includes byte , short , int , long , float , double ,
Boolean and char.
5. What is type casting. Why it is required.
Typecast is a way of changing an object from one data type to the next. It is
used in computer programming to ensure a function handles the variables
correctly. A typecast example is the transformation of an integer into a string.
Type casting helps a programmer convert any given data type to any other data
type in a program.
6. Differentiate between while loop and do loop.

While Loop Do While Loop

The Keyword “while” is used. The keyword “do while” is used.

The control statement is executed The control statement is present


before the loop body. after the loop body.

The loop body executes when the The loop body executes once, even
condition becomes true. if the condition is true or false.

It follows a top-down approach. It follows a bottom-up approach.

For single statements, brackets are not


Brackets are always required.
required.

7. Differentiate between break and continue statement in java.


BASIS FOR
BREAK CONTINUE
COMPARISON

Task It terminates the execution of It terminates only the current iteration

remaining iteration of the loop. of the loop.

Control after 'break' resumes the control of the 'continue' resumes the control of the

break/continue program to the end of loop program to the next iteration of that

enclosing that 'break'. loop enclosing 'continue'.

Causes It causes early termination of loop. It causes early execution of the next

iteration.

Continuation 'break' stops the continuation of 'continue' do not stops the continuation

loop. of loop, it only stops the current

iteration.

Other uses 'break' can be used with 'switch', 'continue' can not be executed with

'label'. 'switch' and 'labels'.

8. What is conditional operator. Give an example.


The logical AND and logical OR operators both take two operands. Each operand is a
boolean expression (i.e., it evaluates to either true or false). The logical AND condition
returns true if both operands are true, otherwise, it returns false. The logical OR condition
returns false if both operands are false, otherwise, it returns true.
int number = 16;
if (number % 2 == 0 && number % 4 == 0)
{System.out.println("It's divisible by two and four!"); }
else
{ System.out.println("It's not divisible by two and four!"); }

9. String functions(library functions discussed in class)


Classes and Objects, Threads and Exceptions
1. Write the properties of a constructor. How it is different from a method.
Constructor have following special features:
I. A constructor name must be same as that of its class name.
II. Constructors are called automatically when the objects are created.
III. Constructors should be declared in the public section to be availabile to all
the functions.

• A Constructor is a block of code that initializes a newly created object.


• A Method is a collection of statements which returns a value upon its
execution.
• A Constructor can be used to initialize an object.
• A Method consists of Java code to be executed.
• A Constructor doesn’t have a return type.
• A Method must have a return type.
• A Constructor initializes an object that doesn’t exist.
• A Method does operations on an already created object.
2. What is constructor overloading. Give an example. [note]
When we declare two constructors with same name this is called as
constructor over loading.
we have created a class Person that has a single variable age. We have also
defined two constructors Person () and Person (int a)

3. What is method overloading. How it is different from method overriding.


Method overloading is used to increase the readability of the program.
Method overriding is used to provide the specific implementation of the
method that is already provided by its super class.
2) Method overloading is performed within class. Method overriding
occurs in two classes that have IS-A (inheritance) relationship.
3) In case of method overloading, parameter must be different. In case of
method overriding, parameter must be same.
4) Method overloading is the example of compile time polymorphism.
Method overriding is the example of run time polymorphism.
1) Method overloading is used to increase the Method overriding is used to provide the
readability of the program. specific implementation of the method
that is already provided by its super class.

2) Method overloading is performed within Method overriding occurs in two


class. classes that have IS-A (inheritance)
relationship.

3) In case of method overloading, parameter In case of method overriding, parameter


must be different. must be same.

4) Method overloading is the example Method overriding is the example of run


of compile time polymorphism. time polymorphism.

4. Write the characteristics of static members in a class.


• A static method can access only static data. It cannot access non-static data
(instance variables) .
• A static method can call only other static methods and cannot call a non-static
method from it .
• A static method can be accessed directly by the class name and doesn't need
any object .

5. What is instance variable. How it is different from static variable.


Instance variables are created when an object is created with the use of the
keyword 'new' and destroyed when the object is destroyed.
Static variables are created when the program starts and destroyed when the
program stops.
6. What is this keyword. Give an example showing this keyword.
This. key word is used to refer to the member to the current class. It is used to
avoid the confusion between the data member and the local variable if they
use the same name.
this. Name=name
7. What is super keyword. Write the different uses of super keyword.
The super keyword refers to superclass (parent) objects. It is used to call
superclass methods, and to access the superclass constructor. The most
common use of the super keyword is to eliminate the confusion between
super classes and subclasses that have methods with the same name.
Usage of Java super Keyword
1. super can be used to refer immediate parent class instance variable.
2. super can be used to invoke immediate parent class method.
3. super() can be used to invoke immediate parent class constructor.

8. What do you mean by final class.


The final class is a class that is declared with the final keyword. Subclasses
can't inherit a final class or a final class cannot be inherited by any subclass. So,
we can restrict class inheritance by making use of a final class.
9. What is an abstract class.
class which is declared with the abstract keyword is known as an abstract class
in Java. It can have abstract and non-abstract methods (method with the
body).
o An abstract class must be declared with an abstract keyword.
o It can have abstract and non-abstract methods.
o It cannot be instantiated.
o It can have constructors and static methods also.
o It can have final methods which will force the subclass not to change the
body of the method.

10. Differentiate between final method and abstract method.


11. What do you mean by dynamic method dispatch.
Dynamic Method Dispatch in Java is the process by which a call to an
overridden method is resolved at runtime (during the code execution).
The concept of method overriding is the way to attain runtime
polymorphism in Java. During the code execution, JVM decides which
implementation of the same method should be called.
12. What is an interface. How it is different from a class.
In Java, an interface specifies the behaviour of a class by providing an abstract
type.

Class Interface

It can contain constructors. It cannot contain constructors.

It contains abstract methods


It cannot contain abstract methods.
only.

Variables and methods in a class can be declared All variables and methods in an
using any access specifier(public, private, default, interface are declared as
protected). public.

13. List the different access specifiers in java.


The four access modifiers in Java are public, protected, default, and private.
14. What is a thread. How it is created in java.
A thread in Java is the direction or path that is taken while a program is being
executed. Generally, all the programs have at least one thread, known as the
main thread, that is provided by the JVM or Java Virtual Machine at the
starting of the program's execution.
You can create threads by implementing the runnable interface and overriding
the run() method. Then, you can create a thread object and call the start()
method. Thread Class: The Thread class provides constructors and methods for
creating and operating on threads.
15. What is multithreading in java.
n Java, Multithreading refers to a process of executing two or more threads
simultaneously for maximum utilization of the CPU. A thread in Java is
a lightweight process requiring fewer resources to create and share the process
resources.

16. Describe the different states that a thread goes through during its lifetime.
1. NEW – a newly created thread that has not yet started the
execution
2. RUNNABLE – either running or ready for execution but it's waiting
for resource allocation
3. BLOCKED – waiting to acquire a monitor lock to enter or re-enter a
synchronized block/method
4. WAITING – waiting for some other thread to perform a particular
action without any time limit
5. TIMED_WAITING – waiting for some other thread to perform a
specific action for a specified period
6. TERMINATED – has completed its execution

17. Draw the state transition diagram of thread life cycle. [ Note]
18. Which methods are required to go from active state to blocked state of a
thread.
When sleep() method is invoked on a thread to sleep for specified time period,
the thread is out of queue during this time period.
19. Which methods are required to go from blocked state to runnable state of
a thread.
The start() method of the Thread class is used to initiate the execution of a
thread and it goes into runnable state and the sleep() and wait() methods of
the Thread class sends the thread into non runnable state. After non runnable
state, thread again comes into runnable state and starts its execution.
20. What is runnable and running state of a thread.
Running State of a thread where the currently executing in the processor is
said to in a Running state. It is the responsibility of the thread scheduler to give
the thread, time to run.
The second phase of a new-born thread is the execution phase. When the
start() method is called on a the new instance of a thread, it enters into a
runnable state. In the runnable state, thread is ready for execution and is
waiting for availability of the processor (CPU time).
21. What is thread priority.
In Java, a thread's priority is an integer in the range 1 to 10. The larger the
integer, the higher the priority.
Java Thread setPriority() method
1. public static int MIN_PRIORITY: It is the maximum priority of a thread.
The value of it is 1.
2. public static int NORM_PRIORITY: It is the normal priority of a thread.
The value of it is 5.
3. public static int MAX_PRIORITY: It is the minimum priority of a thread.
The value of it is 10.
22. What is an exception. Give an example. [ Note]
An exception (or exceptional event) is a problem that arises during
the execution of a program. When an Exception occurs the normal
flow of the program is disrupted and the program/Application
terminates abnormally, which is not recommended, therefore, these
exceptions are to be handled.
23. Describe the exception handling mechanism in java.
24. What is finally statement in java. Give an example.
The finally block in java is used to put important codes such as clean up code
e.g. closing the file or closing the connection. The finally block executes
whether exception rise or not and whether exception handled or not. A finally
contains all the crucial statements regardless of the exception occurs or not.
o finally block in Java can be used to put "cleanup" code such as closing a
file, closing connection, etc.
o The important statements to be printed can be placed in the finally
block.
25. List some of the most common types of exceptions that might occur in java
• NullPointerException. A NullPointerException is thrown when a Java
program attempts to process an object which contains a null value. ...
• ArrayIndexOutOfBoundsException. ...
• IllegalStateException. ...
• ClassCastException. ...
• ArithmeticException. ...
• IllegalArgumentException.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy