Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views5 pages

Module 1

The document covers Java basics, including features of object-oriented programming such as encapsulation, inheritance, and polymorphism. It explains type conversion versus type casting, array declarations, short-circuit operators, sorting elements with a for loop, types of if statements, and differences between procedural and object-oriented programming. Additionally, it includes examples of using the ternary operator to find the greatest of three numbers.

Uploaded by

mb8532633
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)
9 views5 pages

Module 1

The document covers Java basics, including features of object-oriented programming such as encapsulation, inheritance, and polymorphism. It explains type conversion versus type casting, array declarations, short-circuit operators, sorting elements with a for loop, types of if statements, and differences between procedural and object-oriented programming. Additionally, it includes examples of using the ternary operator to find the greatest of three numbers.

Uploaded by

mb8532633
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/ 5

Module 1 – Java Basics (10 Marks Answers)

1. Explain any three features of object-oriented programming.


Answer:
Object-Oriented Programming (OOP) focuses on objects and classes. Three key features are:
1. Encapsulation:
o Wrapping data and code together as a single unit.
o Example:
java
CopyEdit
class Student {
private int marks;
public void setMarks(int m) { marks = m; }
public int getMarks() { return marks; }
}
o marks is hidden (private) and accessed only via methods.
2. Inheritance:
o Acquiring properties and behaviors from another class.
o Promotes code reuse.
o Example:
java
CopyEdit
class Animal {
void eat() { System.out.println("eating"); }
}
class Dog extends Animal {
void bark() { System.out.println("barking"); }
}
3. Polymorphism:
o Ability to take many forms.
o Method Overloading (compile-time) and Overriding (run-time).
o Example:
java
CopyEdit
class Shape {
void draw() { System.out.println("drawing"); }
}
class Circle extends Shape {
void draw() { System.out.println("drawing circle"); }
}

2. Type conversion vs. type casting with examples.


Answer:
Type Conversion is automatic (widening), while Type Casting is manual (narrowing).
1. Type Conversion (Implicit):
java
CopyEdit
int a = 10;
double b = a; // int to double
System.out.println(b); // Output: 10.0
2. Type Casting (Explicit):
java
CopyEdit
double x = 10.5;
int y = (int) x; // double to int
System.out.println(y); // Output: 10
Feature Type Conversion Type Casting
Performed by Compiler (Automatic) Programmer (Manual)
Safety Safe Risk of data loss
Direction Lower to Higher (int→long) Higher to Lower (double→int)

3. 1D and 2D array declaration and initialization in Java.


Answer:
1D Array:
java
CopyEdit
int[] arr = new int[5];
arr[0] = 10;
arr[1] = 20;
// OR
int[] arr2 = {10, 20, 30, 40, 50};
2D Array:
java
CopyEdit
int[][] matrix = new int[2][3];
matrix[0][0] = 1;
// OR
int[][] mat = {
{1, 2, 3},
{4, 5, 6}
};
Program Example:
java
CopyEdit
public class ArrayDemo {
public static void main(String[] args) {
int[] oneD = {1, 2, 3};
int[][] twoD = {{1, 2}, {3, 4}};
System.out.println("1D: " + oneD[1]);
System.out.println("2D: " + twoD[1][0]);
}
}

4. Short-circuit operators with examples.


Answer:
Short-circuit operators are:
• && (AND)
• || (OR)
They evaluate conditions from left to right and stop once the result is known.
Example:
java
CopyEdit
int a = 5, b = 0;
if (b != 0 && a/b > 1) {
System.out.println("Valid division");
} else {
System.out.println("Division not allowed");
}
• Here, b != 0 is false → second condition is skipped → avoids error.

5. Write Java code to sort elements using a for loop.


Answer:
Using Bubble Sort:
java
CopyEdit
public class SortExample {
public static void main(String[] args) {
int[] arr = {5, 2, 8, 1, 3};
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
for (int num : arr) System.out.print(num + " ");
}
}

6. Explain types of if statements.


Answer:
Java provides:
1. Simple if:
java
CopyEdit
if (a > b) System.out.println("A is greater");
2. if-else:
java
CopyEdit
if (a > b) System.out.println("A");
else System.out.println("B");
3. if-else-if ladder:
java
CopyEdit
if (a > b) {...}
else if (a == b) {...}
else {...}
4. Nested if:
java
CopyEdit
if (a > b) {
if (a > c) System.out.println("A is largest");
}

7. Difference between Procedural and Object-Oriented Programming.


Feature Procedural Object-Oriented
Focus Functions Objects
Data Security Low High (Encapsulation)
Code Reusability Less More (Inheritance, Polymorphism)
Example C Java, C++

8. Ternary operator with example to find greatest of three.


Answer:
Ternary Operator: condition ? value1 : value2
Example:
java
CopyEdit
public class TernaryExample {
public static void main(String[] args) {
int a = 20, b = 30, c = 10;
int max = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
System.out.println("Greatest: " + max);
}
}

You might also like