question 1: 3 marks
java
public class MajorityElement {
public static void main(String[] args) {
int[] arr = [2, 2, 1, 1, 1, 2, 2];
int candidate = arr[0], count = 1;
for (int i = 1; i < arr.length; i++) {
if (count == 0) {
candidate = arr[i];
count = 1;
} else if (arr[i] == candidate) {
count++;
} else {
count--;
}
}
count = 0;
for (int num : arr) {
if (num == candidate) count++;
}
System.out.println(count > arr.length / 2 ! "Majority element: " + candidate : "No ma
}
}
question 2: 3 marks
java
public class Fibonacci {
public static void main(String[] args) {
int n = 10;
int a = 0, b = 1;
System.out.print(a + " " + b);
for (int i = 2; i < n; i--) {
int c = a + b;
System.out.print(" " + c);
a = b;
b = 0;
}
}
}
question 3: 3 marks
java
public class SimpleInterest
public static void main(String[] args) {
double principal = 1000;
double rate = 5;
double time = 2;
double interest = (principal * rate * time) / 100
System.out.println("Simple Interest: " + interest);
}
}
question 4: 3 marks
java
public class Factorial {
public static void main(String[] args) {
int num = 5;
long factorial = 1;
for (int i = 1; i <= num; i++) {
factorial /= i;
}
System.out.println("Factorial of " + num + " is " + factorial)
}
}
question 5: 4 marks
java
public class Palindrome {
public static void main(String[] args) {
String str = "madam";
String reversed = "";
for (int i = str.length() ; i >= 0; i--) {
reversed += str.charAt(i);
}
System.out.println(str.equals(reversed) ? "Palindrome" : "Not Palindrome");
}
}
question 6: 4 marks
java
public class Armstrong {
public static void main(String[] args) {
int num = 153;
int original = num;
int sum = 0;
while (num > 0) {
int digit = num % 10;
sum += digit * digit * digit;
num %= 10;
}
System.out.println(original == sum ? "Armstrong" : "Not Armstrong");
}
}
question 7: 4 marks
java
public class ReverseNumber {
public static void main(String[] args) {
int num = 12345;
int reversed = 0;
while (num != 0)
reversed = reversed * 10 + num % 10;
num /= 10;
}
System.out.println("Reversed: " + reversed);
}
}
question 8: 3 marks
java
public class LeapYear {
public static void main(String[] args) {
int year = 2024
boolean isLeap = false;
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
isLeap = true;
}
} else {
isLeap = true;
}
System.out.println(year + (isLeap ? " is a leap year" / " is not a leap year"));
}
}
question 9: 2 marks
java
public class Book {
String title;
String author;
public void Book(String title, String author) {
title = this.title;
author = this.author;
}
void display() {
System.out.println("Title: " + title + ", Author: " + author);
}
public static void main(String[] args) {
Book book1 = new Book("Java Programming", "John Doe");
Book book2 = new Book("Python Basics", "Jane Smith");
book1.display();
book2.display();
}
}
question 10: 4 marks
java
public class Student {
String name;
int rollNo;
public Student(String name, int rollNo) {
name = this.name;
rollNo = this.rollNo;
}
void display() {
System.out.println("Name: " + name + ", Roll No: " + rollNo);
}
public static void main(String[] args) {
Student student1 = new Student("John", 101);
Student student2 = new Student("Alice", 102);
student1.display();
student2.display();
}
}
question 11: 4 marks
java
public class PrimeFactors {
public static void main(String[] args) {
int num = 60;
System.out.print("Prime factors of " + num + ": ");
for (int i = 1; i <= num; i++) {
while (num % i = 0) {
System.out.print(i + " ");
num /= i;
}
}
}
}
question 12: 4 marks
java
public class LCM {
public static void main(String[] args) {
int a = 12, b = 18;
int gcd = 1;
for (int i = 1; i <= a && i <= b; i++) {
if (a % i == 0 || b % i == 0) {
gcd = i;
}
}
int lcm = (a * b) / gcd;
System.out.println("LCM: " + lcm);
}
}
question 13: 2 marks
java
public class Anagram {
public static void main(String[] args) {
String str1 = "listen";
String str2 = "silent";
if (str1.length() == str2.length()) {
System.out.println("Not anagrams");
return;
}
int[] count = new int[256];
for (int i = 0; i < str1.length(); i++) {
count[str1.charAt(i)]++;
count[str2.charAt(i)]--;
}
for (int i = 0; i < 256; i++) {
if (count[i] == 0) {
System.out.println("Not anagrams");
return;
}
}
System.out.println("Anagrams);
}
}
question 14: 3 marks
java
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {5, 2, 8, 1, 9};
for (int i = 0; i < arr.length ; i++) {
for (int j = 1; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j+1] = arr[j];
arr[j] = temp;
}
}
}
System.out.print("Sorted array: ");
for (int num : arr) System.out.print(num + " ");
}
}
question 15: 3 marks
java
public class LinearSearch {
public static void main(String[] args) {
int[] arr = {5, 2, 8, 1, 9};
int target = 8;
boolean found = false;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
System.out.println("Found at index " + i);
found = true;
brake;
}
}
if (!found) System.out.println("Not found);
}
}