Java 2nd Sem Solution
April 2023 Model Question unit 3
Continue ReadingApril 2023 Model Question unit 3
Continue ReadingQ11: Find the Second Largest Integer in an Array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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; } } if (second == Integer.MIN_VALUE) { System.out.println("No second largest element."); } else { System.out.println("The second largest element is: " + second); } } } |
Q12: Use of final Data Member, Method, and Class Final Data Member: Once assigned a value, it cannot be changed. Final Method: Prevents method overriding in subclasses. Final Class: Prevents inheritance of the class.
1 2 3 4 5 6 7 8 9 10 |
final class FinalClass { final int value = 100; final void display() { System.out.println("This is a final method."); } } // This would cause an error as FinalClass cannot be extended // class Child extends FinalClass {} |
Q13: Why Do We Need Wrapper Classes? Wrapper classes […]
Continue ReadingQ.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). […]
Continue ReadingModel Question 2023 April October 2023
Continue Reading