OOPs Solution
OOPs Solution
1. What are syntax errors, runtime errors, and logic errors? Discuss with
examples.
--> Syntax Errors:
These are mistakes in writing the code that break the rules of the Java language.
System.out.println("Hello, World!")
The program compiles successfully, but crashes or behaves unexpectedly during execution.
Often caused by dividing by zero, accessing invalid array indexes, or incorrect input.
System.out.println(result);
These errors don't cause the program to crash, but the output is incorrect or unexpected.
The program runs smoothly, but the logic or formula used is wrong.
Usually, it's a mistake in thinking or planning.
int num1 = 5;
int num2 = 3;
int sum = num1 * num2; // Wrong logic: Used multiplication instead of addition
i) Arithmetic Operators:
+ (Addition)
- (Subtraction)
* (Multiplication)
/ (Division)
% (Modulus — Remainder)
Example:
int a = 10;
int b = 3;
System.out.println(a + b); // 13
System.out.println(a - b); // 7
System.out.println(a * b); // 30
== (Equal to)
Example :
int x = 5;
int y = 8;
Example :
boolean a = true;
boolean b = false;
= (Assign)
Example :
System.out.println(num); // 15
System.out.println(num); // 30
v) Unary Operators:
+ (Positive)
- (Negative)
++ (Increment)
-- (Decrement)
! (Logical NOT)
Example : int a = 5;
System.out.println(+a); // 5
System.out.println(-a); // -5
System.out.println(++a); // 6 (Increment: a = a + 1)
System.out.println(--a); // 5 (Decrement: a = a - 1)
| (Bitwise OR)
^ (Bitwise XOR)
~ (Bitwise NOT)
Example :
Syntax:
Example:
int a = 10;
int b = 20;
System.out.println(result); // B is greater
The Java program (.class file) is platform-independent, meaning it can run on any OS. However, the
JVM that executes this program is specific to the OS. Each OS has a different way of handling system
resources, memory, and files, so a separate JVM is needed for each platform.
👉 Conclusion: The bytecode is platform-independent, but the JVM that runs it is platform-
dependent.
Java has several key features that make it popular and powerful:
Platform-Independent: Write once, run anywhere — Java bytecode runs on any JVM.
Secure: Provides features like bytecode verification, no pointers, and built-in security.
Portable: Java programs can be easily moved and run on different systems.
OOP is a programming style that uses objects and classes to organize code. It helps in creating
modular, reusable, and efficient code.
Inheritance: Acquiring properties from a parent class (child inherits from parent).
Encapsulation: Hiding data and providing access through methods (like a capsule).
Abstraction: Hiding complex details and showing only essentials (like driving a car without knowing
its internal mechanism).
Automatically done by Java when converting a smaller data type to a larger one.
🔹 Example:
System.out.println(result); // 10.0
ii. Explicit Type Casting (Narrowing)
🔹 Example:
2. Write a single Java program that demonstrates the usage of the following
keywords: import,new, this, break, continue.
Code : // import keyword: Importing Scanner class for user input
import java.util.Scanner;
class Example {
int num;
Example(int num) {
this.num = num;
void showNumbers() {
if (i == 5) {
if (i == 8) {
break; // Stop loop when i reaches 8
System.out.println(i);
scanner.close();
Explanation of Keywords:
When primitive data types (int, float, char, etc.) are passed, Java creates a copy of the variable.
Any changes made inside the method do not affect the original variable.
Example:
class Test {
int x = 20;
obj.modify(x);
🔹 Output:
Inside method: 30
Outside method: 20
👉 Here, x remains 20 outside the method because Java passes a copy of the value.
Java does not support pass by reference, but when an object reference is passed, changes made
inside the method reflect on the original object because both point to the same memory location.
Example:
class Example {
int num;
Example(int num) {
this.num = num;
🔹 Output:
Before method: 20
After method: 30
👉 Here, obj.num changes because the method modifies the same object in memory.
A method in Java is a block of code that performs a specific task. It helps in code reusability and
modularity.
Syntax of a Method:
returnType methodName(parameters) {
// method body
return a + b;
return a + b + c;
return a + b;
Copy Constructor → Creates a new object by copying values from another object.
Example:
class Student {
String name;
int age;
// Parameterized Constructor
Student(String n, int a) {
name = n;
age = a;
void display() {
s1.display();
s2.display();
🔹 Output:
Name: Alice, Age: 20
👉 Here, the constructor initializes name and age for each student object when it is created.
return a + b;
return a + b + c;
return a + b;
O/P :
15
30
8.0
class Animal {
void sound() {
// Child Class
// Overriding method
@Override
void sound() {
System.out.println("Dog barks");
}
public static void main(String[] args) {
O/P:
Dog barks
Dog barks
// Child Class
class Dog extends Animal {
// Overriding method
@Override
void makeSound() {
System.out.println("Dog barks");
}
🔹 Output:
1. Definition
o An abstract class is a class that can have both abstract (without body) and concrete
(with body) methods. It is used to provide a base for other classes to extend.
o An interface is a completely abstract type that only contains abstract methods
(before Java 8) and default/static methods (since Java 8). It defines a contract that
classes must follow.
2. Method Implementation
o In an abstract class, methods can be fully implemented, partially implemented, or
completely abstract.
o In an interface, all methods are public and abstract by default (except static/default
methods in Java 8+).
3. Inheritance
o A class can extend only one abstract class (single inheritance).
o A class can implement multiple interfaces, allowing multiple inheritance in Java.
4. Variables
o An abstract class can have instance variables (with or without final and static).
o An interface can only have public, static, and final variables (constants).
5. Constructors
o Abstract classes can have constructors.
o Interfaces cannot have constructors because they cannot be instantiated.
6. Usage
o Use abstract classes when you want to share common behavior among multiple
related classes.
o Use interfaces when you want to define a common contract that multiple classes
from different hierarchies can implement.
4. Explain the Comparable and Cloneable interfaces in Java.
1. Comparable Interface
2. Cloneable Interface
Key Differences
Data Representation:
File Size:
Text I/O: Larger file size due to character encoding (e.g., UTF-8).
Binary I/O: Smaller file size as it stores raw data directly.
Readability:
Usage:
Text I/O: Used for storing plain text, logs, configuration files, etc.
Binary I/O: Used for images, videos, audio, and serialized objects.
Byte streams in Java are used for reading and writing binary data such as images, audio,
video, and non-text files. They work with raw bytes and do not perform character encoding.
Example :
import java.io.*;
int i;
while ((i = fis.read()) != -1) { // Read byte by byte
Character streams are used for reading and writing text files (i.e., .txt, .csv). Unlike byte
streams, they handle characters instead of raw bytes, making them ideal for text-based data.
import java.io.*;
int i;
}
fr.close(); // Close the stream