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

Java Viva Question 2

Uploaded by

sanandu300
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)
14 views

Java Viva Question 2

Uploaded by

sanandu300
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/ 10

JAVA VIVA QUESTIONS

Q: What is Java?
A: Java is a high-level, object-oriented programming language developed by Sun
Microsystems. It is designed to be platform-independent and follows the "Write Once, Run
Anywhere" (WORA) principle.
Q: Explain the main features of Java.
A: Key features include platform independence, object-oriented, simple and familiar, robust
and secure, architecture-neutral, dynamic, distributed computing, and multithreaded.
Q: What are the primitive data types in Java?
A: The primitive data types in Java are byte, short, int, long, float, double, char, and boolean.
Q: Explain the difference between float and double.
A: float is a 32-bit single-precision floating-point type, while double is a 64-bit double-
precision floating-point type. double provides more precision but uses more memory.
Q: What is the difference between break and continue statements?
A: The break statement is used to terminate the loop or switch statement, while the continue
statement is used to skip the current iteration of a loop and continue with the next iteration.
Q: Explain the switch statement.
A: The switch statement is used to select one of many code blocks to be executed. It
evaluates an expression, matches the expression value to a case label, and executes the
corresponding block of code.
4. Classes and Objects:
Q: What is a class in Java?
A:A class is a blueprint for objects. It defines the properties and behaviors that objects created
from the class will have.
Q: Explain the difference between a class and an object.
A: A class is a template or blueprint for creating objects, while an object is an instance of a
class with specific values.
Q: What is inheritance in Java?
A: Inheritance is a mechanism in which a new class inherits properties and behaviors from an
existing class. It promotes code reusability.
Q: Explain polymorphism.
A: Polymorphism allows objects of different types to be treated as objects of a common type.
It can be achieved through method overloading or method overriding.
Q: What is an exception in Java?
A: An exception is an event that disrupts the normal flow of a program. It occurs when an
error or exceptional condition is encountered during runtime.
Q: Explain the difference between checked and unchecked exceptions.
A: Checked exceptions are checked at compile-time, and the compiler forces you to either
handle them using try-catch blocks or declare them in the method signature. Unchecked
exceptions are not checked at compile-time, and the programmer is not required to handle
them.
7. Multithreading:
Q: What is multithreading in Java?
A: Multithreading is a feature that allows multiple threads of execution to run concurrently
within the same program.
Q: How can you create a thread in Java?
A: You can create a thread by extending the Thread class or implementing the Runnable
interface and then instantiating and starting the thread.
Q: What is a package in Java?
A: A package is a way of organizing related classes and interfaces into a single unit. It helps
in avoiding naming conflicts and provides a way to encapsulate and group related classes.
Q: Why are packages used in Java?
A: Packages are used to organize and manage classes in a modular and hierarchical manner.
They help in avoiding naming conflicts, improving code reusability, and providing a clear
structure to the code.
Q: How do you declare a package in Java?
A: To declare a package, you include the package statement at the beginning of the Java
source file. For example:
package mypackage;
Q: How can you import classes from a different package?
A: You can use the import statement to import classes from other packages. For example:
import mypackage.*;
or
import mypackage.classname;
Q: Explain the access modifiers in Java.
A: Java provides four access modifiers: public, protected, default (friendly), and private.
Private protected. They control the visibility of classes, methods, and variables within and
outside the package.
Q: What is the default access level for members in a package?
A: The default access level (package-friendly) allows members to be accessed only within
the same package. It is used when no access modifier is specified.
Q: Can a class belong to multiple packages?
A: No, a class in Java can belong to only one package. However, a package can contain
multiple classes and subpackages.
Q: What is the purpose of the java.util package?
A: The java.util package contains utility classes and interfaces for data structures (such as
lists, sets, and maps), date and time manipulation, and other general-purpose utilities.
Q: Explain the role of the java.io package.
A: The java.io package provides classes for input and output operations. It includes classes
for reading and writing streams, files, and other I/O-related tasks.
Q: What is a Java Applet?
A: A Java Applet is a small program that is embedded in an HTML page and is executed by a
web browser. Applets are used to create dynamic and interactive web content.
Q: How does an applet differ from a standalone Java application?
A: An applet is designed to be embedded within an HTML page and executed within a web
browser. It extends the Applet class, whereas a standalone Java application is executed
independently and typically has a main method.
Q: Explain the life cycle of an applet.
A: The life cycle of an applet involves four methods: init(), start(), stop(), and destroy(). The
init() method is called when the applet is initialized, start() is called when the applet is started
or resumed, stop() is called when the applet is stopped or paused, and destroy() is called when
the applet is about to be destroyed.
Q: Why is the paint() method important in applets?
A: The paint() method is called automatically whenever the applet needs to be redrawn. It is
crucial for rendering graphics and updating the visual appearance of the applet.
Q: How do you embed an applet in an HTML page?
A: To embed an applet in an HTML page, you use the <applet> tag. For example:
htmlCopy code
<applet code="MyApplet.class" width="300" height="200"></applet>
Q: What are the essential attributes in the <applet> tag?
A: The essential attributes include code (specifies the applet's class file), width (specifies the
width of the applet in pixels), and height (specifies the height of the applet in pixels).
Q: Explain the concept of parameters in applets.
A: Parameters in applets are specified in the <param> tags within the <applet> tag. They are
used to pass initialization data to the applet and can be accessed using the getParameter()
method.
Q: Why are applets considered a security risk?
A: Applets are considered a security risk because they have the potential to access the local
file system, network resources, and other sensitive operations. Modern web browsers often
restrict the capabilities of applets to mitigate these security concerns.
Q: How can you digitally sign an applet to enhance security?
A: Applets can be digitally signed using a digital signature to prove their authenticity and
integrity. This allows them to access restricted resources, and users are prompted to grant
permission for the signed applet to run.
Q: What is multithreading?
A: Multithreading is the concurrent execution of two or more threads within the same
program. Each thread runs independently, and multiple threads can execute concurrently,
allowing for better utilization of CPU resources.
Q: How does multithreading differ from multitasking?
A: Multitasking is a broader term that refers to the concurrent execution of multiple tasks or
processes, which can include multithreading. Multithreading specifically involves the
concurrent execution of threads within a single process.
Q: How can you create a thread in Java?
A: There are two ways to create a thread in Java: by extending the Thread class and by
implementing the Runnable interface. Extending the Thread class involves creating a new
class that extends Thread and overriding its run() method. Implementing Runnable involves
creating a class that implements the Runnable interface and defining the run() method.
Q: What is the start() method used for in multithreading?
A: The start() method is called to begin the execution of a thread. It internally calls the run()
method, and a new thread of execution is started.
Q: Explain the various states of a thread.
A: A thread in Java can be in one of the following states:
New: When a thread is created but not yet started.
Runnable: When a thread is ready to run and waiting for CPU time.
Blocked: When a thread is waiting for a monitor lock to enter a synchronized block or
method.
Waiting: When a thread is waiting for another thread to perform a particular action.
Terminated: When a thread has exited or completed its execution.
Q: What is the significance of the sleep() method in multithreading?
A: The sleep() method is used to pause the execution of a thread for a specified amount of
time, allowing other threads to execute. It helps in introducing delays or controlling the
timing of threads.
Q: What is synchronization in multithreading?
A: Synchronization is the process of controlling the access of multiple threads to shared
resources. It ensures that only one thread can access a critical section of code at a time,
preventing data inconsistency and race conditions.
Q: How can threads communicate with each other in Java?
A: Threads can communicate using methods like wait(), notify(), and notifyAll() provided by
the Object class. These methods enable threads to coordinate their execution and
synchronization.
Q: What is Swing in Java?
A: Swing is a set of GUI components for Java that provides a rich set of user interface
elements, including buttons, text fields, labels, and more. It is part of the Java Foundation
Classes (JFC) and allows developers to create platform-independent, interactive graphical
user interfaces.
Q: How does Swing differ from AWT (Abstract Window Toolkit)?
A: Swing is an extension of AWT with additional features and components. Unlike AWT,
Swing components are lightweight, platform-independent, and provide a more sophisticated
and customizable look and feel.
2. Swing Components:
Q: Name some commonly used Swing components.
A: Commonly used Swing components include JButton, JTextField, JLabel, JCheckBox,
JRadioButton, JComboBox, JList, JTable, JTextArea, and JScrollPane.
Q: What is the purpose of the JFrame class in Swing?
A: The JFrame class is a top-level container that represents the main window of a Swing
application. It provides the basic functionalities of a window, such as title, border, and layout
management.
Q: What is a Layout Manager in Swing?
A: A Layout Manager in Swing is responsible for defining the arrangement and sizing of
components within a container. It helps in creating flexible and platform-independent GUIs
by automatically adjusting the layout based on the container's size.
Q: Name a few layout managers in Swing.
A: Some layout managers in Swing include FlowLayout, BorderLayout, GridLayout,
CardLayout, BoxLayout, and GridBagLayout.
Q: How is event handling achieved in Swing?
A: Event handling in Swing is achieved through the use of listeners. Components generate
events (e.g., button clicks, key presses), and corresponding listener interfaces (e.g.,
ActionListener, MouseListener, KeyListener) are implemented to respond to these events.
Q: What is the purpose of the ActionEvent class in Swing?
A: The ActionEvent class represents an action event, such as a button click. It is commonly
used with ActionListener to handle user actions.
Q: What is inheritance in Java?
A: Inheritance is a mechanism in Java by which a class acquires the properties and behaviors
of another class. The class that is inherited is called the superclass or parent class, and the
class that inherits is called the subclass or child class.
Q: What is the main purpose of inheritance in object-oriented programming?
A: The main purpose of inheritance is to promote code reusability and establish a
relationship between classes, allowing the subclass to inherit the attributes and behaviors of
the superclass.
Q: Explain single inheritance, multiple inheritance, and multilevel inheritance.
A:
Single Inheritance: A class can inherit from only one superclass.
Multiple Inheritance: A class can inherit from multiple superclasses. Java supports multiple
inheritance through interfaces, but not through classes.
Multilevel Inheritance: A class is derived from another class, and then another class is derived
from it.
Q: Why does Java not support multiple inheritance through classes?
A: Multiple inheritance through classes can lead to the "diamond problem" (ambiguity when
a class inherits from two classes that have a common ancestor). To avoid this, Java supports
multiple inheritance through interfaces.
Q: What is method overriding?
A: Method overriding is the process by which a subclass provides a specific implementation
for a method that is already defined in its superclass. It allows a subclass to provide a
specialized version of a method that is already defined in its superclass.
Q: What are the rules for method overriding in Java?
A:
The method in the subclass must have the same signature as the method in the superclass
(same name, return type, and parameters).
The access level of the overriding method cannot be more restrictive than the overridden
method.
The overriding method can throw the same, subclass, or no exception compared to the
overridden method.
Q: What is the purpose of the super keyword in Java?
A: The super keyword is used to refer to the superclass or parent class. It is used to access
the members of the superclass, call the superclass's constructor, and distinguish between
overridden methods and subclass methods.
Q: How is the super() constructor call different from this() constructor call?
A: The super() constructor call is used to invoke the constructor of the immediate superclass,
while this() is used to invoke the constructor of the same class or an overloaded constructor
within the same class.
Q: What is an abstract class?
A: An abstract class is a class that cannot be instantiated on its own and may contain abstract
methods (methods without a body). Subclasses of an abstract class must provide concrete
implementations for its abstract methods.
Q: How does an interface support multiple inheritance in Java?
A: An interface in Java allows a class to inherit from multiple interfaces, providing a way to
achieve multiple inheritance. A class can implement multiple interfaces.
Q: How is the final keyword used in the context of inheritance?
A:
When applied to a class: It indicates that the class cannot be subclassed.
When applied to a method: It indicates that the method cannot be overridden by subclasses.
When applied to a variable: It indicates that the variable is a constant and cannot be
reassigned.
Q: What is the significance of the super() method in a constructor?
A: The super() method is used to call the constructor of the immediate superclass. It should
be the first statement in the constructor of the subclass. If not explicitly called, the default
constructor of the superclass is implicitly invoked.
Q: What is an interface in Java?
A: An interface in Java is a collection of abstract methods. It defines a contract of methods
that a class implementing the interface must provide. It is a way to achieve abstraction and
multiple inheritance in Java.
Q: How is an interface different from a class?
A:An interface can only have abstract methods and constants (public static final fields).
A class can have fields (variables), constructors, and methods with or without
implementations.
A class can extend only one class, but it can implement multiple interfaces.
Q: How do you declare an interface in Java?
A: To declare an interface, use the interface keyword. For example:
public interface MyInterface
{
void myMethod();
}
Q: How is an interface implemented in a class?
A: To implement an interface, a class uses the implements keyword followed by the interface
name. The class must provide concrete implementations for all the abstract methods declared
in the interface.
Q: What is a default method in an interface?
A: A default method in an interface is a method with a default implementation. It allows
adding new methods to an existing interface without breaking the classes that already
implement the interface.
Q: When would you use a static method in an interface?
A: Static methods in interfaces are used for utility methods that are related to the interface but
don't depend on an instance of the implementing class. They are invoked using the interface
name.
Q: Can an interface extend another interface? A: Yes, an interface can extend another
interface using the extends keyword. The extending interface inherits the abstract methods of
the parent interface.
6. When to Use Interfaces vs. Abstract Classes:
Q: When would you prefer using an interface over an abstract class? A: Use interfaces when:
You want to achieve multiple inheritance.
You need to define a contract for multiple classes.
You want to create a common API that can be implemented by various classes.
Q: Can a class implement multiple interfaces in Java?
A: Yes, a class can implement multiple interfaces in Java. This feature allows a class to
inherit from multiple sources, achieving a form of multiple inheritance.
Q: What is JDBC?
A: JDBC (Java Database Connectivity) is a Java API that enables Java applications to interact
with databases. It provides a standard interface for connecting to relational databases and
performing database operations.
Q: What are the key components of JDBC architecture?
A: The key components of JDBC architecture include the JDBC API, JDBC drivers,
DriverManager, Connection, Statement, ResultSet, and SQLException.
Q: What are the different types of JDBC drivers?
A: There are four types of JDBC drivers:
Type 1 (JDBC-ODBC Bridge Driver): Uses ODBC to connect to databases.
Type 2 (Native-API Driver): Converts JDBC calls into database-specific calls using native
APIs.
Type 3 (Network Protocol Driver): Communicates with a middle-tier server that converts
JDBC calls into a database-specific protocol.
Type 4 (Thin Driver or Direct-to-Database Driver): Communicates directly with the database
using a database-specific protocol.
Q: Which type of JDBC driver is considered the most preferred and why?
A: Type 4 (Thin Driver or Direct-to-Database Driver) is considered the most preferred
because it communicates directly with the database server, eliminating the need for an
intermediary translation layer. It is platform-independent and provides better performance.
Q: How do you establish a connection to a database using JDBC?
A: You can establish a connection to a database using the DriverManager.getConnection()
method.
For example:
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username",
"password");
Q: What is the role of the Class.forName() method in JDBC?
A: The Class.forName() method is used to dynamically load and register the JDBC driver.
For example:
Class.forName("com.mysql.cj.jdbc.Driver");
Q: How do you execute a simple SQL query in JDBC?
A: You can execute a SQL query using the Statement interface. For example:
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM mytable");
Q: Explain the difference between Statement, PreparedStatement, and CallableStatement.
A: Statement: Used for general-purpose SQL queries.
PreparedStatement: Precompiles SQL statements, providing better performance and
preventing SQL injection.
CallableStatement: Used to call stored procedures in the database.

You might also like