Q.no 11
1 2 3 4 5 6 7 8 |
public class LogicalOperatorsDemo { public static void main(String[] args) { boolean a = true, b = false; System.out.println("a && b: " + (a && b)); // false System.out.println("a || b: " + (a || b)); // true System.out.println("!a: " + (!a)); // false } } |
Q.no 12
1 2 3 4 5 6 7 8 9 10 11 |
public class SecondLargest { public static void main(String[] args) { int[] arr = {12, 35, 1, 10, 34, 1}; int first = Integer.MIN_VALUE, second = Integer.MIN_VALUE; for (int num : arr) { if (num > first) { second = first; first = num; } else if (num > second && num != first) { second = num; } } System.out.println("Second largest: " + (second == Integer.MIN_VALUE ? "Not found" : second)); } } |
Q.no 13
1 2 3 4 5 6 7 8 9 10 11 12 |
public class MethodOverloadingExample { public int add(int a, int b) { return a + b; } public int add(int a, int b, int c) { return a + b + c; } public double add(double a, double b) { return a + b; } public static void main(String[] args) { MethodOverloadingExample obj = new MethodOverloadingExample(); System.out.println(obj.add(5, 10)); // 15 System.out.println(obj.add(5, 10, 15)); // 30 System.out.println(obj.add(5.5, 4.5)); // 10.0 } } |
Q.no 14
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.Arrays; public class GenericSwap { public static <T> void swap(T[] array, int i, int j) { T temp = array[i]; array[i] = array[j]; array[j] = temp; } public static void main(String[] args) { Integer[] arr = {1, 2, 3, 4, 5}; swap(arr, 1, 3); System.out.println(Arrays.toString(arr)); // [1, 4, 3, 2, 5] } } |
Q.no 15
1 2 3 4 5 6 7 |
public class FactorialRecursion { public static int factorial(int n) { return (n <= 1) ? 1 : n * factorial(n - 1); } public static void main(String[] args) { System.out.println("Factorial: " + factorial(5)); // Example input: 5 -> Output: 120 } } |
Group C
Q.No 17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
// Shape.java interface Shape { double area(); double perimeter(); } // Circle.java class Circle implements Shape { private double radius; public Circle(double radius) { this.radius = radius; } @Override public double area() { return Math.PI * radius * radius; } @Override public double perimeter() { return 2 * Math.PI * radius; } } // Rectangle.java class Rectangle implements Shape { private double length, breadth; public Rectangle(double length, double breadth) { this.length = length; this.breadth = breadth; } @Override public double area() { return length * breadth; } @Override public double perimeter() { return 2 * (length + breadth); } } // Sample.java public class Sample { public static void main(String[] args) { Shape circle = new Circle(7); Shape rectangle = new Rectangle(5, 10); System.out.println("Circle Area: " + circle.area()); System.out.println("Circle Perimeter: " + circle.perimeter()); System.out.println("Rectangle Area: " + rectangle.area()); System.out.println("Rectangle Perimeter: " + rectangle.perimeter()); } } |
Q.No 18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
// Student.java class Student { private String name; private double percentage; public Student(String name, double percentage) { this.name = name; this.percentage = percentage; } public String getName() { return name; } public double getPercentage() { return percentage; } } // StudentDemo.java public class StudentDemo { public static void main(String[] args) { Student[] students = { new Student("arbind", 85.5), new Student("Shyam", 92.0), new Student("Hari", 78.5), new Student("Rahul", 88.0), new Student("Proshesh", 79.5), new Student("Kabi", 95.0), new Student("Sucess", 89.5), new Student("Nutan", 91.0), new Student("Jagdish", 94.5), new Student("Rajeet", 80.0) }; Student topStudent = students[0]; for (Student student : students) { if (student.getPercentage() > topStudent.getPercentage()) { topStudent = student; } } System.out.println("Student with the highest average marks: " + topStudent.getName()); System.out.println("Percentage: " + topStudent.getPercentage()); } } |
Q.No 19
1 2 3 4 5 6 7 8 9 10 |
public class ArrayExample { public static void main(String[] args) { try { int[] numbers = {1, 2, 3}; System.out.println(numbers[5]); // Invalid index } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Error: Array index out of bounds!"); } } } |
Q.No 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.io.*; public class FileReadWrite { public static void main(String[] args) { // Reading data from "text.txt" try (BufferedReader reader = new BufferedReader(new FileReader("text.txt")); BufferedWriter writer = new BufferedWriter(new FileWriter("best.txt"))) { String line; while ((line = reader.readLine()) != null) { writer.write(line); writer.newLine(); } System.out.println("Data successfully written to best.txt"); } catch (IOException e) { System.out.println("An error occurred: " + e.getMessage()); } } } |
Group D
Q.No 21
OOP Principles Followed by Java:
- Encapsulation: Wrapping data (fields) and methods in a single unit (class).
- Abstraction: Hiding implementation details and exposing only essential features.
- Inheritance: Mechanism to acquire properties and behaviors of a parent class.
- Polymorphism: Ability to perform a single action in different ways (e.g., method overloading and overriding).
Restrictions of Static Methods:
- Static methods belong to the class rather than an object.
- They cannot access instance variables or instance methods directly.
- They cannot use
this
orsuper
keywords. - They can only call other static methods or use static variables.
Java Program Demonstrating Static Method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class StaticExample { private static int count = 0; public static void incrementCount() { count++; } public static int getCount() { return count; } } public class StaticDemo { public static void main(String[] args) { StaticExample.incrementCount(); StaticExample.incrementCount(); System.out.println("Count: " + StaticExample.getCount()); } } |
Q.no 22
Definition of Inheritance:
Inheritance is a mechanism in object-oriented programming where one class (child/subclass) inherits properties and behaviors (fields and methods) from another class (parent/superclass).
Advantages of Inheritance:
- Code Reusability: Allows reuse of existing code.
- Extensibility: New features can be added without modifying existing code.
- Polymorphism: Enables dynamic method invocation.
- Reduced Redundancy: Avoids duplication of code.
Types of Inheritance in Java:
- Single Inheritance:
- A subclass inherits from a single parent class.
- Example:
Multilevel Inheritance:
A class inherits from another class, and that class inherits from another class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
class Grandparent { void showGrandparent() { System.out.println("This is the grandparent class."); } } class Parent extends Grandparent { void showParent() { System.out.println("This is the parent class."); } } class Child extends Parent { void showChild() { System.out.println("This is the child class."); } } public class MultilevelInheritanceDemo { public static void main(String[] args) { Child child = new Child(); child.showGrandparent(); child.showParent(); child.showChild(); } } |
Hierarchical Inheritance:
Multiple classes inherit from a single parent class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
class Parent { void display() { System.out.println("This is the parent class."); } } class Child1 extends Parent { void showChild1() { System.out.println("This is child 1."); } } class Child2 extends Parent { void showChild2() { System.out.println("This is child 2."); } } public class HierarchicalInheritanceDemo { public static void main(String[] args) { Child1 child1 = new Child1(); child1.display(); child1.showChild1(); Child2 child2 = new Child2(); child2.display(); child2.showChild2(); } } |