0% found this document useful (0 votes)
15 views8 pages

Java Exam Prep: Key Concepts & Code

The document is a Java subject exam preparation guide covering key concepts such as the basic structure of a Java program, features of Java, OOP concepts, JVM, exception handling, and file operations. It includes code examples, flashcards for quick revision, and explanations of various Java topics like inheritance, constructors, and thread management. The content is organized into sections with questions and answers to facilitate learning and exam readiness.

Uploaded by

howareyu494
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)
15 views8 pages

Java Exam Prep: Key Concepts & Code

The document is a Java subject exam preparation guide covering key concepts such as the basic structure of a Java program, features of Java, OOP concepts, JVM, exception handling, and file operations. It includes code examples, flashcards for quick revision, and explanations of various Java topics like inheritance, constructors, and thread management. The content is organized into sections with questions and answers to facilitate learning and exam readiness.

Uploaded by

howareyu494
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

Java Subject Exam Preparation

Q.1 (a) Basic Structure of Java Program (3 Marks)

java
class MyClass {
public static void main(String[] args) {
[Link]("Hello World");
}
}
Main Points:
- class defines the class.
- main() is the entry point.
- [Link]() prints output.

Flashcard:
Q: What is the entry point of a Java program?
A: public static void main(String[] args)

Q.1 (b) Features of Java (4 Marks)

List:
- Simple
- Object-Oriented
- Platform Independent
- Secure
- Robust
- Portable

Any Two:
- Platform Independent: Java code runs on any OS using JVM.
- Object-Oriented: Everything is based on classes and objects.

Flashcard:
Q: Which Java feature makes it OS-independent?
A: Platform Independent

Q.1 (c) Sum of Digits Program (7 Marks)

java
import [Link];
class SumDigits {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int num = [Link](), sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
[Link]("Sum: " + sum);
}
}
Java Subject Exam Preparation

Flashcard:
Q: Java loop used to extract digits from a number?
A: while loop with % and /

Q.1 (c) OR: Max of Ten Numbers (7 Marks)

java
class MaxTen {
public static void main(String[] args) {
int max = [Link](args[0]);
for (int i = 1; i < [Link]; i++) {
int num = [Link](args[i]);
if (num > max) max = num;
}
[Link]("Max: " + max);
}
}

Flashcard:
Q: How to get values from command line in Java?
A: Using args[]

Q.2 (a) OOP Concepts (3 Marks)

List:
- Inheritance
- Polymorphism
- Encapsulation
- Abstraction

Example (Encapsulation):
Wrapping data and code in one unit (class) using private variables and public methods.

Flashcard:
Q: What hides internal details and shows only functionality?
A: Abstraction

Q.2 (b) JVM (4 Marks)

JVM = Java Virtual Machine


- Executes Java bytecode
- Provides platform independence
- Manages memory (GC)

Flashcard:
Q: What runs Java bytecode?
A: JVM

Q.2 (c) Constructor Overloading (7 Marks)


Java Subject Exam Preparation

java
class Demo {
Demo() { [Link]("No-arg"); }
Demo(int x) { [Link]("Value: " + x); }
}

Flashcard:
Q: What is constructor overloading?
A: Multiple constructors with different parameters

Q.2 OR (a) Wrapper Class (3 Marks)

Converts primitive to object types.


Example: int a = 5; Integer obj = a;

Flashcard:
Q: Convert int to Integer?
A: Integer obj = a;

Q.2 (b) Static Keyword (4 Marks)

Used to define class-level members.


java
class A {
static int x = 10;
}

Flashcard:
Q: Use of static?
A: Belongs to class, not object

Q.2 (c) Copy Constructor (7 Marks)

java
class Demo {
int x;
Demo(int a) { x = a; }
Demo(Demo d) { x = d.x; }
}

Flashcard:
Q: What copies data from one object to another?
A: Copy constructor

Q.3 (a) String Functions (3 Marks)

Methods:
- length()
- charAt()
- substring()
Java Subject Exam Preparation

- equals()

Flashcard:
Q: String method to get a character?
A: charAt(index)

Q.3 (b) Types of Inheritance (4 Marks)

- Single
- Multilevel
- Hierarchical

Multilevel Example:
class A { }
class B extends A { }
class C extends B { }

Flashcard:
Q: A B C is which inheritance?
A: Multilevel

Q.3 (c) Interface & Multiple Inheritance (7 Marks)

java
interface A { void show(); }
interface B { void display(); }
class C implements A, B {
public void show() {}
public void display() {}
}

Flashcard:
Q: Can Java use multiple inheritance using classes?
A: No, only via interfaces

Q.3 OR (a) this Keyword (3 Marks)

Refers to current object.


java
class A {
int x;
A(int x) { this.x = x; }
}

Flashcard:
Q: Purpose of this?
A: Refers to current object

Q.3 (b) Method Overriding (4 Marks)


Java Subject Exam Preparation

java
class A { void show() { [Link]("A"); } }
class B extends A { void show() { [Link]("B"); } }

Flashcard:
Q: What is method overriding?
A: Redefining parent method in subclass

Q.3 (c) Package (7 Marks)

Steps:
- Write package mypack;
- Save file
- Compile: javac -d . [Link]
- Run: java [Link]

Flashcard:
Q: Keyword to define package?
A: package

Q.4 (a) Thread Priority (3 Marks)

Thread priorities range from 1 to 10.


Example: [Link](Thread.MAX_PRIORITY);

Flashcard:
Q: Default thread priority?
A: 5

Q.4 (b) Thread & Lifecycle (4 Marks)

States: New Runnable Running Blocked Terminated

Flashcard:
Q: State after thread starts?
A: Runnable

Q.4 (c) Thread Class Program (7 Marks)

java
class MyThread extends Thread {
public void run() { [Link]("Running"); }
public static void main(String args[]) {
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
[Link]();
[Link]();
}
}
Java Subject Exam Preparation

Flashcard:
Q: Method to start a thread?
A: start()

Q.4 OR (a) Exception Handling Concept (3 Marks)

Process of handling runtime errors using try, catch, finally.

Flashcard:
Q: Java block for error handling?
A: try-catch

Q.4 (b) Multiple Catch (4 Marks)

java
try {
int a = 1/0;
} catch (ArithmeticException e) {
[Link]("Math error");
} catch (Exception e) {
[Link]("General error");
}

Flashcard:
Q: Can multiple catch blocks be used?
A: Yes

Q.4 (c) Arithmetic Exception Program (7 Marks)

java
public class Test {
public static void main(String[] args) {
try {
int x = 5 / 0;
} catch (ArithmeticException e) {
[Link]("Divide by zero");
}
}
}

Flashcard:
Q: Which exception for divide by zero?
A: ArithmeticException

Q.5 (a) ArrayIndexOutOfBound (3 Marks)

Occurs when accessing array index out of bounds.


Example:
int[] a = {1,2,3};
[Link](a[5]);
Java Subject Exam Preparation

Flashcard:
Q: Accessing invalid index in array gives?
A: ArrayIndexOutOfBoundsException

Q.5 (b) Stream Classes (4 Marks)

Java IO streams:
- InputStream
- OutputStream
- FileReader
- FileWriter

Flashcard:
Q: Class for byte input?
A: InputStream

Q.5 (c) Write to Text File (7 Marks)

java
import [Link].*;
class WriteFile {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("[Link]");
[Link]("Hello Java");
[Link]();
}
}

Flashcard:
Q: Class to write text files?
A: FileWriter

Q.5 OR (a) Divide by Zero Exception (3 Marks)

Occurs when a number is divided by zero.


Example: int a = 5 / 0;

Flashcard:
Q: Result of divide by zero?
A: ArithmeticException

Q.5 (b) Try-Catch Block (4 Marks)

Used to handle exceptions.


try {
int a = 1/0;
} catch (Exception e) {
[Link](e);
}
Java Subject Exam Preparation

Flashcard:
Q: Which block catches exceptions?
A: catch

Q.5 (c) Display & Append File (7 Marks)

java
import [Link].*;
class FileAppend {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("[Link]"));
String line;
while ((line = [Link]()) != null) [Link](line);
[Link]();

FileWriter fw = new FileWriter("[Link]", true);


[Link]("\nNew Line");
[Link]();
}
}

Flashcard:
Q: File append class?
A: FileWriter with true flag

Common questions

Powered by AI

Encapsulation in object-oriented programming involves wrapping data (variables) and methods (functions) that operate on the data into a single unit, known as a class. By using private variables and providing public methods, encapsulation helps protect the integrity of the object's data. For example, a class with `private int age;` and a method `public void setAge(int age) { this.age = age; }` demonstrates encapsulation . Abstraction, on the other hand, refers to the concept of hiding complex reality while exposing only the necessary parts. It involves focussing on essential qualities of something rather than specific characteristics. Abstraction can be achieved using interfaces and abstract classes, for example, an interface containing `void start();` without implementation shows abstraction, letting different classes implement the interface while providing specific details .

Overloaded constructors in Java are multiple constructors within the same class that have different parameter lists. They allow the creation of objects in different ways, depending on the given inputs. For example, `class Demo { Demo() {}; Demo(int x) {}; }` showcases overloaded constructors . A copy constructor is a specific type of constructor that creates a new object as a copy of an existing object. It typically takes an object of the same class as a parameter and copies its data fields into the new object, such as `Demo(Demo d) { x = d.x; }` . While overloaded constructors serve to provide multiple initialization options, a copy constructor specifically serves to duplicate an object while maintaining its current state .

Java does not support multiple inheritance through classes due to the complexity and ambiguity that arise, such as the diamond problem, where a class could inherit conflicting properties from multiple parent classes. However, interfaces provide a workaround. A class can implement multiple interfaces, allowing it to inherit the abstract methods defined in them. This ensures that the design remains clear and unambiguous because interfaces do not contain any implementation until a class implements them, thus resolving the typical issues associated with multiple inheritance .

The Java Virtual Machine (JVM) plays a crucial role in making Java platform-independent. It executes Java bytecode, which is compiled from Java source code, rather than compiling directly into machine code. Because every operating system has its own JVM implementation, the same bytecode can run on any OS that has a compatible JVM. This allows Java programs to be written once and run anywhere .

The 'this' keyword in Java is a reference to the current object within an instance method or a constructor. It is primarily used to differentiate between instance variables and parameters with the same name, such as in a constructor or method. For example, in a constructor `A(int x) { this.x = x; }`, 'this.x' refers to the instance variable, while 'x' refers to the constructor parameter . Other uses include returning the current class instance and invoking other methods in the same class .

The 'package' keyword in Java is used to define a grouping of similar classes and interfaces. It aids in organizing code into a folder structure, thereby avoiding naming conflicts and improving access control. To implement a package, follow these steps: (1) Declare the package at the top of your source file with `package mypack;`. (2) Save the file in a directory structure that matches the package name. (3) Compile with `javac -d . file.java` to set the package structure. (4) Run using `java mypack.ClassName` to access the compiled class .

The method 'public static void main(String[] args)' is the entry point of any Java program. The 'public' keyword allows it to be accessible from outside its class, 'static' means it can be called without creating an instance of the class, and 'void' indicates that it doesn't return any value. The 'main' method accepts a single argument which is an array of Strings ('String[] args'), used to pass command-line arguments at runtime .

Multilevel inheritance in Java allows a class to inherit from another subclass, forming an inheritance chain, which helps in reusing code and creating a clearer class hierarchy. For instance, if class C extends class B, which extends class A, class C inherits the properties and methods of both B and A. This form of inheritance is useful in scenarios where an object is a specific type of a more general class, e.g., a class 'Animal' can be extended by 'Mammal', which can further be extended by 'Dog', enhancing code organization and promoting logical structures .

Wrapper classes in Java provide a way to use primitive data types (int, boolean, etc.) as objects. This is essential in collections where objects are required. By converting primitives to objects, Java ensures type safety by allowing collection classes to enforce type checking. For instance, an `int` can be converted to an `Integer` using `Integer obj = a;` for use in collections like ArrayList. Additionally, wrapper classes provide utility methods for type conversion, parsing, and object manipulation, aiding in effective memory management .

Exception handling in Java involves using the try-catch block to manage runtime errors gracefully without crashing the program. The 'try' block contains code that might throw an exception, while the 'catch' block handles specific exceptions if they occur. Multiple catch blocks can be used when different error handling is needed for different exception types, enhancing the process by allowing more precise handling of various types of errors. For example, one catch block can handle `ArithmeticException` and another can handle `Exception` for more general errors, providing more fine-grained control over error handling .

You might also like