Q11: 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 are used to:
- Convert primitive types into objects (Autoboxing).
- Work with collections (e.g.,
ArrayList
requires objects, not primitives). - Provide utility methods (e.g.,
Integer.parseInt()
,Double.valueOf()
).
1 2 3 4 5 6 7 8 9 10 |
public class WrapperExample { public static void main(String[] args) { int num = 10; // Primitive Integer wrappedNum = num; // Autoboxing int unwrapped = wrappedNum; // Unboxing System.out.println("Wrapped: " + wrappedNum); System.out.println("Unwrapped: " + unwrapped); } } |
Q14: Check if a Character Is an Alphabet
1 2 3 4 5 6 7 8 9 10 |
public class AlphabetCheck { public static void main(String[] args) { char c = 'A'; if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { System.out.println(c + " is an alphabet."); } else { System.out.println(c + " is not an alphabet."); } } } |
Q15: Example Program of Inner Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class OuterClass { private String message = "Hello from Outer Class"; class InnerClass { void displayMessage() { System.out.println(message); } } } public class InnerClassDemo { public static void main(String[] args) { OuterClass outer = new OuterClass(); OuterClass.InnerClass inner = outer.new InnerClass(); inner.displayMessage(); } } |
Q16:Â Containing a Generic Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class GenericBox<T> { private T value; public void setValue(T value) { this.value = value; } public T getValue() { return value; } } public class GenericExample { public static void main(String[] args) { GenericBox<Integer> intBox = new GenericBox<>(); intBox.setValue(100); System.out.println("Integer Value: " + intBox.getValue()); GenericBox<String> strBox = new GenericBox<>(); strBox.setValue("Hello"); System.out.println("String Value: " + strBox.getValue()); } } |
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 |
interface Exam { void setExam(String division, int marks); void showExam(); } class Test implements Exam { private String division; private int marks; @Override public void setExam(String division, int marks) { this.division = division; this.marks = marks; } @Override public void showExam() { System.out.println("Division: " + division + ", Marks: " + marks); } } public class ExamDemo { public static void main(String[] args) { Test test = new Test(); test.setExam("Science", 85); test.showExam(); } } |
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 |
class Book { private String title; private double price; public void setVar(String title, double price) { this.title = title; this.price = price; } public void showVar() { System.out.println("Title: " + title + ", Price: " + price); } } public class BookDemo { public static void main(String[] args) { Book[] books = new Book[4]; books[0] = new Book(); books[0].setVar("Java Basics", 200); books[1] = new Book(); books[1].setVar("Python Guide", 150); books[2] = new Book(); books[2].setVar("Java Advanced", 300); books[3] = new Book(); books[3].setVar("C++ Concepts", 180); System.out.println("Books with titles starting with 'Java':"); for (Book book : books) { if (book != null && book.title.startsWith("Java")) { book.showVar(); } } } } |
Q.no 19
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 |
import java.io.*; class Movie implements Serializable { private int id; private String genre; public Movie(int id, String genre) { this.id = id; this.genre = genre; } @Override public String toString() { return "ID: " + id + ", Genre: " + genre; } } public class MovieDemo { public static void main(String[] args) { Movie comedyMovie = new Movie(1, "Comedy"); try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Comedy.dat"))) { oos.writeObject(comedyMovie); System.out.println("Movie object written to Comedy.dat"); } catch (IOException e) { e.printStackTrace(); } } } |