1. What is Bytecode in Java?
Bytecode in Java is an intermediate representation of a Java program. When Java source code is
compiled, it is not converted directly to machine code. Instead, it is compiled into bytecode by the
Java compiler. Bytecode is a set of instructions that can be executed by the Java Virtual Machine
(JVM).
Advantages of Bytecode:
• Platform Independence: Bytecode can run on any platform with a JVM.
• Security: The JVM verifies bytecode before execution.
Example:
// Compiling the code below generates bytecode (MyClass.class)
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello, World!");
2. List Any Five Features of Java
1. Platform Independence: Write once, run anywhere due to the use of bytecode and JVM.
2. Object-Oriented: Focuses on objects, classes, inheritance, and polymorphism.
3. Robust: Features like garbage collection and exception handling make Java reliable.
4. Multithreaded: Java supports concurrent execution of multiple threads.
5. Secure: Includes a runtime environment with security features like bytecode verification.
3. Difference Between while Loop and do-while Loop in Java
Feature while Loop do-while Loop
Condition Condition is checked before executing the Condition is checked after executing the
Check loop. loop.
Execution Executes 0 or more times. Executes at least once.
Syntax while(condition) { //code } do { //code } while(condition);
Example:
// While Loop Example
int i = 1;
while (i <= 3) {
System.out.println(i);
i++;
// Do-While Loop Example
int j = 1;
do {
System.out.println(j);
j++;
} while (j <= 3);
4. Types of Constructors in Java
1. Default Constructor: A constructor with no arguments, initializes default values.
2. class Example {
3. Example() {
4. System.out.println("Default Constructor");
5. }
6. }
7. Parameterized Constructor: Accepts arguments to initialize specific values.
8. class Example {
9. Example(int x) {
10. System.out.println("Value: " + x);
11. }
12. }
13. Copy Constructor: Initializes an object by copying data from another object.
14. class Example {
15. int x;
16. Example(Example e) {
17. this.x = e.x;
18. }
19. }
5. Commonly Used Methods of the File Class
1. createNewFile(): Creates a new file.
2. File file = new File("example.txt");
3. file.createNewFile();
4. delete(): Deletes a file.
5. File file = new File("example.txt");
6. file.delete();
7. mkdir(): Creates a new directory.
8. File dir = new File("exampleDir");
9. dir.mkdir();
10. renameTo(): Renames a file or directory.
11. File oldFile = new File("example.txt");
12. File newFile = new File("newExample.txt");
13. oldFile.renameTo(newFile);
6. Java Program to Display Array Elements in Ascending Order
import java.util.Arrays;
import java.util.Scanner;
public class SortArray {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = scanner.nextInt();
int[] arr = new int[n];
System.out.println("Enter elements:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
Arrays.sort(arr);
System.out.println("Sorted Array: " + Arrays.toString(arr));
7. Concept of Inheritance in Java
Inheritance allows one class (child) to acquire the properties and methods of another class (parent).
Example:
class Parent {
void display() {
System.out.println("This is the parent class.");
class Child extends Parent {
void show() {
System.out.println("This is the child class.");
Code Reusability: Inheritance reduces redundancy by reusing existing class properties.
8. Visibility Modifiers in Java
1. private: Accessible only within the same class.
2. default (package-private): Accessible within the same package.
3. protected: Accessible within the same package and subclasses.
4. public: Accessible from any class.
9. Role of final Keyword
1. Final Variable: Value cannot be changed.
2. final int x = 10;
3. Final Method: Cannot be overridden.
4. final void display() {}
5. Final Class: Cannot be extended.
6. final class Example {}
10. Primitive Data Types in Java
1. Integer Types: byte, short, int, long.
2. Floating-Point Types: float, double.
3. Character Type: char.
4. Boolean Type: boolean.
11. Multithreading and Thread Lifecycle
Multithreading allows concurrent execution of multiple threads.
Thread Lifecycle:
• New
• Runnable
• Running
• Blocked
• Terminated
Example Using Runnable:
class MyThread implements Runnable {
public void run() {
System.out.println("Thread is running.");
public class Main {
public static void main(String[] args) {
Thread thread = new Thread(new MyThread());
thread.start();
12. Concept of Arrays
• Declaration: int[] arr;
• Initialization: arr = new int[5];
• Access: arr[0] = 10;
Example:
int[] arr = {1, 2, 3};
System.out.println(arr[0]);
13. File Handling Using File Class
import java.io.File;
import java.io.IOException;
public class FileHandlingDemo {
public static void main(String[] args) {
try {
// Creating a file
File file = new File("example.txt");
if (file.createNewFile()) {
System.out.println("File created");
// Deleting a file
File deleteFile = new File("delete.txt");
if (deleteFile.delete()) {
System.out.println("File deleted");
// Renaming a file
File oldFile = new File("oldname.txt");
File newFile = new File("newname.txt");
if (oldFile.renameTo(newFile)) {
System.out.println("File renamed");
// Creating a directory
File directory = new File("newFolder");
if (directory.mkdir()) {
System.out.println("Directory created");
} catch (IOException e) {
System.out.println("An error occurred");
}
14. Control Structures: if-else and switch
if-else Example:
if (x > 0) {
System.out.println("Positive");
} else {
System.out.println("Negative");
switch Example:
switch (day) {
case 1: System.out.println("Monday"); break;
default: System.out.println("Other");
15. Purpose of this Keyword
Used to refer to the current class object.
Example:
class Example {
int x;
Example(int x) {
this.x = x;