Java 1 & 2 Marks Important Questions With Answers Object Oriented Concepts Using Java Programming

1 & 2 Marks (IMP)

1. What is Java?

Java is a high-level, object-oriented, platform-independent programming language and computing platform. It was developed by Sun Microsystems (now owned by Oracle) in 1995 and is widely used for building robust, secure, and portable applications.

2. What is Java Environment?

Java environment includes a number of development tools, classes and methods. The development tools are part of the system known as Java Development Kit (JDK) and the classes and methods are part of the Java Standard Library (JSL), also known as the Application Programming Interface (API).

3. What is JDK? List the components.

JDK (Java Development Kit) is a software development environment used for developing Java applications and applets. It includes tools needed to write, compile, debug, and run Java programs.

Components of JDK:

  1. JRE (Java Runtime Environment) – Runs Java programs

  2. JVM (Java Virtual Machine) – Executes Java bytecode

4. list out the Features of Java.
  • Object-Oriented: Uses classes and objects.

  • Platform-Independent: “Write Once, Run Anywhere” using the Java Virtual Machine (JVM).

  • Simple & Secure: Designed to eliminate common programming errors.

  • Multithreaded: Supports multiple threads of execution.

  • Robust & Portable: Reliable and runs on various platforms.

5. Define Java compiler & Interpreter?
  • Java Compiler: Definition: The Java Compiler (javac) is a program that translates Java source code (.java files) into bytecode (.class files). Bytecode is a platform-independent, intermediate representation of the code that can be executed by the Java Virtual Machine (JVM).
  • Java Interpreter (JVM): The Java Interpreter (part of the JVM) is a software component that executes Java bytecode line-by-line at runtime. It translates bytecode into machine-specific instructions for the underlying operating system.
6. Write a simple Java Program.
class Demo 
{
public static void main(String args[])
{
System.out.println (“Welcome to JAVA”);
}
}
7. Mention the tokens of Java.
  1. Literals
  2. Identifiers
  3. Keywords
  4. Operators
  5. Separators
8. Define JVM (Java Virtual Machine).

JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime environment in which java bytecode can be executed.

9. Define Constant.

Constant means fixed value which is not change at the time of execution of program.

10. What is Variable? list out the types.

Variable is name of reserved area allocated in memory. In other words, it is a name of memory location. It is a combination of “vary + able” that means its value can be changed.

Types of Variables:

  • local variable
  • Instance variable
  • Static variable
11. How to Declare and Initialize a Variable in Java.
dataType variableName; // Declaration
variableName = value; // Initialization
12. Define the data types in java.
  • Primitive Data Types
  • Non-Primitive (Reference/Object) Data Types
13. Define Class and Object:
  • Class: A class is a blueprint or template for creating objects. It defines the data (attributes) and behavior (methods) that the objects created from it will have.
  • Object: An object is an instance of a class. It is a real-world entity that has state (attributes) and behavior (methods).
14. List the commands used for compilation and execution of a Java Program

To Compile a Java Program:

javac FileName.java

To Execute a Java Program:

java ClassName
15. Define Polymorphism in Java

Polymorphism is the ability of an object to take on many forms. In Java, it allows methods to perform different tasks based on the object that invokes them. It can be achieved through method overloading and method overriding.

16. Define Method Overriding

Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass using the same name, return type, and parameters.

17. Define Event.

An event in Java is an object that describes a change in the state of a source, such as a button click, mouse movement, or key press. Java uses an event-driven model in GUI programming.

18. Define Stream Classes.

Stream classes in Java are part of the java.io package and are used for input and output operations. They handle reading and writing data to files, memory, and other I/O devices.

19. Define Class.
  • Class is a user defined data type. It is a collection of data and methods.

  • A class is a group of objects that has common properties. It is a template or blueprint from which objects
    are created.

20. What is Constructor? Mention the types of 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.

Types of java constructors:

  1. Default constructor (no-arg constructor)
  2. Parameterized constructor
21. What is Static keyword?

The static keyword in java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than instance of the class.

22. What is Static variable?

If you declare any variable as static, it is known static variable.

  •  The static variable can be used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees,college name of students etc.
  • The static variable gets memory only once in class area at the time of class loading.
23. Why java main method is static?

Because object is not required to call static method if it were non-static method, jvm create object first then call main() method that will lead the problem of extra memory allocation.

24. What is Java static block ?
  • Is used to initialize the static data member.
  • It is executed before main method at the time of classloading.
25. Define Inheritance.
  • Inheritance is one of the feature of Object-Oriented Programming (OOPs).
  • Inheritance can be defined as the process where one class acquires the properties and methods of another class.
26. Define Method Overriding

Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass using the same name, return type, and parameters.

27. Define extends keyword.

extends is the keyword used to inherit the properties of a class. Below given is the syntax of extends keyword.

28. Define Single & Multilevel Inheritance.

Single Inheritance: When a one class extends another one class only then we call it a single inheritance.

Multilevel Inheritance: it is the derived class inherits from one Base class, which intern inherits from some other class is called multilevel inheritance in Java.

29. Define final keyword.

The final keyword in java is used to restrict the user. The java final keyword can be used in many context. Final can be: (variable, method & class)

30. What is Final variable? Example.

If you make any variable as final, you cannot change the value of final variable(It will be constant).

Example:

class Bike 
{
final int speedlimit=90; //final variable void
run()
{
speedlimit=400;
}
}
class FinalVariable
{
public static void main(String args[])
{
Bike obj=new Bike();
obj.run();
}
}
31. What is Java Garbage Collection?
  • In java, garbage means unreferenced objects.
  • Garbage Collection is process of reclaiming the runtime unused memory automatically. In other words, it is a way to destroy the unused objects.
32. What is Array?
  • Array is a collection of similar type of elements that have contiguous memory location.
  • Java array is an object the contains elements of similar data type. It is a data structure where we store similar elements. We can store only fixed set of elements in a java array.
  • Array in java is index based, first element of the array is stored at 0 index.
33. What are the advantages and disadvantages of Java Array?

Advantage of Java Array

  • Easy to use: Arrays are simple to create and use in Java.

  • Fast access: You can quickly access any element using its index.

  • Saves memory: You can store many items in one variable.

  • Organized storage: Keeps related data in one place.

Disadvantage of Java Array

  • Fixed size: Once created, you can’t change the size.

  • Same data type only: You can store only one type of data.

  • No built-in functions: You have to write your own code to add, delete, or search items.

  • Wastes space: If you don’t use all the space, memory is wasted.

  • Can crash: If you try to access wrong index, it gives an error.

34. What are the different types of arrays in Java?
  • One Dimensional Array
  • Two Dimensional Array
  • Multidimensional Array
35. What is String in java?

String is a sequence of characters. But in java, string is an object that represents a sequence of characters. String class is used to create string object.

36. What is the difference between String and StringBuffer in Java?
StringStringBuffer
Immutable (cannot be changed)Mutable (can be changed)
Every change creates a new objectChanges are made in the same object
Slower in performance for modificationsFaster when many modifications are needed
Uses more memory if changed oftenUses less memory for repeated changes
Best for fixed or read-only textBest for frequently changing text
37. What is a Vector in Java?
  • Vectors in Java are part of the legacy collection framework (since Java 1.0) and are similar to ArrayList, but with some key differences. or
  • A Vector in Java is a resizable array that can grow or shrink in size automatically. It is used to store a group of elements and is thread-safe, which means it can be used safely in multi-threaded programs.
38. Define Wrapper class in Java.

Wrapper class in java provides the mechanism to convert primitive data types into object and object into primitive data types.

39. Define interface.

An interface in Java is a collection of abstract methods. It is used to achieve abstraction and multiple inheritance. A class can implement an interface and provide code for its methods.

40. Why use Java interface?

There are mainly three reasons to use interface. They are given below.

  • It is used to achieve fully abstraction.
  • By interface, we can support the functionality of multiple inheritance.
  • It can be used to achieve loose coupling.
41.What are the types of Java packages?
  1. Java API package or Built-in Packages (Predefined Packages)
  2. User defined packages
42. What is Java System Packages?

Java API provides a large number of classes grouped into different packages according to functionality. Most of the time in our programming we use the Java System Packages. 

43. How do you access a package in Java? Write the syntax.

import keyword is used to import built-in and user-defined packages into your java source file. So that your class can refer to a class that is in another package by directly using its name.

Syntax:

import package1.package2.classname; 
44. Define Thread.
  • Thread – Thread is a small block of code, which executes particular task.
  • A thread is a lightweight sub process, a smallest unit of processing. It is a separate path of execution.
45. What is Multitasking?

Multitasking is a process of executing multiple tasks simultaneously. We use multitasking to utilize the CPU. Multitasking can be achieved by two ways:

  • Process-based Multitasking (Multiprocessing)
  • Thread-based Multitasking (Multithreading)
46. What is an error in Java? List the types of errors.

The mistakes which we have done while developing our program, those mistakes are called errors.

Types:

  • Compile time errors
  • Run time errors
47. What is exception? List the types of exception.

A run time errors are called exception. Or abnormal termination of the program is
called exception.

Types:

  • Error
  • Checked Exception
  • Unchecked Exception
48. 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.

49. Define Java try block.
  • Java try block is used to enclose the code that might throw an exception. It must be used within the method.
  • Java try block must be followed by either catch or finally block.
50. Write a syntax of java try-catch?
try 
{
//code that may throw exception
}
catch(Exception_class_Name ref)
{
// Code to handle the exception
}
51. Write a syntax of try-finally block
try 
{
//code that may throw exception
}
finally
{
//Code that will always run (cleanup code)
}
52. What is throw keyword ?

The Java throw keyword is used to explicitly throw an exception. We can throw either checked or unchecked exception in java by throws keyword. The throw keyword is mainly used to throw custom exception. We will see custom exceptions later.

53. Define Applet in Java and mention its usage.

An applet in Java is a small program that runs in a web browser or applet viewer. It is a subclass of java.applet.Applet or javax.swing.JApplet and is mainly used for internet-based applications.

Usage of Applet:

  • Internet Computing – Applets can be sent over the Internet and run on a client’s machine.

  • Graphical Interface – Applets are good for creating interactive graphics, animations, and games.

  • User Interaction – They can respond to mouse clicks, keyboard inputs, etc.

  • Lightweight Programs – They are small and fast to load in web pages.

54. What are the attributes of Applet tags?

1. height: Defines height of applet
2. width: Defines width of applet
3. align: Defines the text alignment around the applet
4. alt: An alternate text to be displayed if the browser support applets but cannot run this applet
5. archive: A URL to the applet when it is stored in a Java Archive or ZIP file
6. code: A URL that points to the class of the applet
7. codebase: Indicates the base URL of the applet if the code attribute is relative
8. hspace: Defines the horizontal spacing around the applet
9. vspace: Defines the vertical spacing around the applet
10. name: Defines a name for an applet
11. object: Defines the resource name that contains a serialized representation of the applet
12. title: Display information in tool tip

55. What is Graphics Programming?

Graphics is one of the most important features of Java. Java applets can be written to draw lines, arcs, figures, images and text in different fonts and styles. Different colors can also be incorporated in display.

56. What are lines and rectangles in Java Applet? Write their syntax.

Lines: Lines are drawn by means of the drawLine() method.

Syntax:

void drawLine(int startX, int startY, int endX, int endY) 

Rectangle: The drawRect() and fillRect() methods display an outlined and filled rectangle, respectively.

Syntax:

void drawRect(int top, int left, int width, int height) void 
fillRect(int top, int left, int width, int height)
57. Define Java AWT and mention its uses.

AWT (Abstract Window Toolkit) is a part of Java’s GUI (Graphical User Interface) library. It is used to create windows, buttons, text fields, labels, and other graphical user interface components in Java applications.

Uses of AWT:

  1. To create Graphical User Interfaces (GUIs).

  2. To design interactive forms with components like Button, Label, TextField, etc.

  3. To handle user events like mouse clicks and key presses.

  4. To draw shapes and graphics (lines, rectangles, circles).

  5. To develop platform-independent window-based applications.

58. What are the common AWT classes in Java?

Common AWT Classes:

  • Frame – Main window

  • Label – Displays text

  • Button – Clickable button

  • TextField – Input box for user text

  • Checkbox – Selection box

  • Panel – Container for components

59. Define Window.

Window is also a SubClass of a Container and window class creates a top level window. These are not directly created, for this the subclass of window name frame is used and dialogs are used.

60. Define Container.

A Container in Java is a special component that can hold and organize other GUI components like buttons, text fields, labels, etc. It acts as a parent to other components and helps in arranging them in a window.

61. Define Panel.

A Panel is a lightweight container in Java used to group and organize a set of GUI components inside a window. It helps to divide the window into sections and manage components better.

62. What is Stream Classes?

Stream classes in Java are used to perform input and output (I/O) operations. They provide a way to read data from a source (like a file, keyboard, or network) or write data to a destination (like a file or screen) in a continuous flow called a stream.

Types of Streams:

  1. Byte stream classes
  2. Character stream classes 
63. What is the java.io package?

The java.io package in Java provides a large number of classes and interfaces for input and output (I/O) operations. It allows programs to read data from and write data to different sources like files, memory, or network connections.

64. State methods of graphics class.

The graphics class in Java provides various methods to draw shapes, text, and images on components like Applets, Frames, or Panels.

Methods:

drawLine(int x1, int y1, int x2, int y2) 
drawRect(int x, int y, int width, int height)
fillRect(int x, int y, int width, int height)
drawOval(int x, int y, int width, int height)
65. What is typecasting in Java?

Typecasting (or type conversion) is the process of converting a variable from one data type to another. It allows you to treat a variable as a different type.

Java supports two types of typecasting:

  1. Implicit (Widening) Casting 

  2. Explicit (Narrowing) Casting

66. What is encapsulation?

Encapsulation is the process of wrapping data (variables) and methods (functions) into a single unit called a class, and restricting direct access to some of the object’s components to protect the data.

67. What are command line arguments? Give example.

Command line arguments are inputs passed to a Java program when it is started from the command line or terminal. They allow users to provide data to the program at runtime without changing the code.

Example:

public class CommandLineExample 
{
public static void main(String[] args)
{
System.out.println("Number of arguments: " + args.length);
for (int i = 0; i < args.length; i++)
{
System.out.println("Argument " + i + ": " + args[i]);
}
}
}
68. State any two classes used for file input and output.
  • FileInputStream

    • Used to read bytes from a file (for input).

  • FileOutputStream

    • Used to write bytes to a file (for output).

69. State the packages that need to be imported to create an applet.
import java.applet.Applet;
import java.awt.*;
  • java.applet.Applet provides the base class for applets.

  • java.awt is used for graphical components like buttons, text fields, and drawing.

70. What are access modifiers in Java ?

Access modifiers in Java are keywords used to set the visibility or access level of classes, methods, and variables. They control which parts of a program can use or modify those members.

71. How Destructors are defined in Java?

Java does not have destructors like some other languages (e.g., C++). Instead, Java uses a method called finalize() that the garbage collector calls before reclaiming an object’s memory.

Syntax:

protected void finalize() throws Throwable 
{
// cleanup code
super.finalize();
}
72. What is the Significance of the "super" Keyword in Java?

The super keyword in Java is used to refer to the immediate parent class of the current object. It helps in accessing parent class members (variables, methods, and constructors) from a subclass.

Uses of super keyword:

  1. Access Parent Class Variable

  2. Invoke Parent Class Method

  3. Call Parent Class Constructor

73. What is Package? Mention any 2 advantages using package.

A package in Java is a collection of related classes and interfaces grouped together under a common name. It helps organize code and avoid naming conflicts.

Advantages:

  • Helps to organize classes: keeps related classes together.

  • Prevents name conflicts: allows classes with the same name in different packages.

74. What is byte stream?
  • A Byte Stream in Java is used to perform input and output of 8-bit bytes.
  • Used for reading/writing binary data like images or audio files.

Example: FileInputStream, FileOutputStream

75. Define Border Layout.

BorderLayout is one of the layout managers provided by the AWT (Abstract Window Toolkit) package. It is used to arrange components in five regions: North, South, East, West, and Center.

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    error: Content is protected !!