Day 3 - core java

 1. Multithreading

2. final keyword

3. super keyword

4. this keyword

5. static keyword

6. ArrayList

7. HashMap


Questions:

1. What is Java?

2. What are the main features of Java?

3. What is the difference between JDK, JRE, and JVM?

4. What is the difference between an object and a class?

5. What are the access modifiers in Java?

6. What is inheritance in Java?

7. What is the difference between method overloading and method overriding?

8. What is the ‘final’ keyword in Java?

9. What is the difference between an array and an ArrayList in Java?

10. What is the difference between String, StringBuffer, and StringBuilder in Java?

11. What is the difference between ‘==’ and ‘equals()’ in Java?

12. What are constructors in Java?

13. What is the difference between a constructor and a method?

14. What are exceptions in Java?

15. What is the difference between checked and unchecked exceptions?

16. What is the purpose of the ‘static’ keyword in Java?

17. What is polymorphism in Java?

18. What is an interface in Java?

19. What is a package in Java?

20. What is the significance of the 'this' keyword in Java?

21. Why Java is platform independent?



Assignment: Explain the program line by line


1. Write a program to reverse a string in Java.

public class ReverseString { public static void main(String[] args) { String str = "Java"; String reversed = ""; for (int i = str.length() - 1; i >= 0; i--) { reversed += str.charAt(i); } System.out.println("Reversed String: " + reversed); } }

2. Write a program to check if a number is prime or not.

public class PrimeNumber { public static void main(String[] args) { int num = 29; boolean isPrime = true; for (int i = 2; i <= num / 2; i++) { if (num % i == 0) { isPrime = false; break; } } if (isPrime) { System.out.println(num + " is a prime number."); } else { System.out.println(num + " is not a prime number."); } } }

3. Write a program to find the factorial of a number.

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); } }

4. Write a program to check if a string is a palindrome.

public class Palindrome { public static void main(String[] args) { String str = "madam"; String reversed = new StringBuilder(str).reverse().toString(); if (str.equals(reversed)) { System.out.println(str + " is a palindrome."); } else { System.out.println(str + " is not a palindrome."); } } }

5. Write a program to print Fibonacci series up to a given number.

public class Fibonacci { public static void main(String[] args) { int n = 10, a = 0, b = 1; System.out.print("Fibonacci series up to " + n + ": "); for (int i = 1; i <= n; i++) { System.out.print(a + " "); int next = a + b; a = b; b = next; } } }

6. Write a program to find the largest of three numbers.

public class LargestNumber { public static void main(String[] args) { int a = 10, b = 20, c = 30; if (a >= b && a >= c) { System.out.println(a + " is the largest."); } else if (b >= a && b >= c) { System.out.println(b + " is the largest."); } else { System.out.println(c + " is the largest."); } } }

7. Write a program to count the number of vowels in a string.

public class VowelCount { public static void main(String[] args) { String str = "Hello World"; int count = 0; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') { count++; } } System.out.println("Number of vowels: " + count); } }

8. Write a program to print a pyramid pattern of stars.

public class Pyramid { public static void main(String[] args) { int rows = 5; for (int i = 1; i <= rows; i++) { for (int j = i; j < rows; j++) { System.out.print(" "); } for (int k = 1; k <= (2 * i - 1); k++) { System.out.print("*"); } System.out.println(); } } }

9. Write a program to find the sum of digits of a number.

public class SumOfDigits { public static void main(String[] args) { int num = 12345, sum = 0; while (num != 0) { sum += num % 10; num /= 10; } System.out.println("Sum of digits: " + sum); } }

10. Write a program to print the prime numbers between 1 and 100.

public class PrimeNumbers { public static void main(String[] args) { for (int num = 2; num <= 100; num++) { boolean isPrime = true; for (int i = 2; i <= num / 2; i++) { if (num % i == 0) { isPrime = false; break; } } if (isPrime) { System.out.print(num + " "); } } } }

11. Write a program to check if a number is Armstrong (Narcissistic) number.

  • An Armstrong number is a number that is equal to the sum of the cubes of its digits (for a 3-digit number).
public class ArmstrongNumber { public static void main(String[] args) { int num = 153, sum = 0, temp, remainder; temp = num; while (temp != 0) { remainder = temp % 10; sum += Math.pow(remainder, 3); temp /= 10; } if (sum == num) { System.out.println(num + " is an Armstrong number."); } else { System.out.println(num + " is not an Armstrong number."); } } }

12. Write a program to remove duplicates from an array.

import java.util.HashSet; import java.util.Set; public class RemoveDuplicates { public static void main(String[] args) { int[] arr = {1, 2, 3, 2, 4, 5, 1, 6}; Set<Integer> set = new HashSet<>(); for (int num : arr) { set.add(num); } System.out.println("Array without duplicates: " + set); } }

13. Write a program to check if a string is an anagram of another string.

import java.util.Arrays; public class AnagramCheck { public static void main(String[] args) { String str1 = "listen"; String str2 = "silent"; char[] charArray1 = str1.toCharArray(); char[] charArray2 = str2.toCharArray(); Arrays.sort(charArray1); Arrays.sort(charArray2); if (Arrays.equals(charArray1, charArray2)) { System.out.println(str1 + " and " + str2 + " are anagrams."); } else { System.out.println(str1 + " and " + str2 + " are not anagrams."); } } }

14. Write a program to find the GCD (Greatest Common Divisor) of two numbers.

public class GCD { public static void main(String[] args) { int a = 56, b = 98, gcd = 1; for (int i = 1; i <= a && i <= b; i++) { if (a % i == 0 && b % i == 0) { gcd = i; } } System.out.println("GCD of " + a + " and " + b + " is: " + gcd); } }

15. Write a program to find the LCM (Least Common Multiple) of two numbers.

public class LCM { public static void main(String[] args) { int a = 12, b = 15; int lcm = (a * b) / findGCD(a, b); System.out.println("LCM of " + a + " and " + b + " is: " + lcm); } static int findGCD(int a, int b) { while (b != 0) { int temp = b; b = a % b; a = temp; } return a; } }

Comments