[go: up one dir, main page]

0% found this document useful (0 votes)
22 views5 pages

Java DSA Test Prep

The document outlines key concepts in Java and Data Structures and Algorithms (DSA) for test preparation. It covers basics such as JVM, JDK, and JRE, along with Java programming fundamentals including loops, arrays, strings, searching, sorting, and problem-solving techniques. Each section includes example code snippets to illustrate the concepts discussed.

Uploaded by

Ketan Sutar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

Java DSA Test Prep

The document outlines key concepts in Java and Data Structures and Algorithms (DSA) for test preparation. It covers basics such as JVM, JDK, and JRE, along with Java programming fundamentals including loops, arrays, strings, searching, sorting, and problem-solving techniques. Each section includes example code snippets to illustrate the concepts discussed.

Uploaded by

Ketan Sutar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Java + DSA Basics – Test Preparation

1. Java: JDK / JVM / JRE Basics


**Q1. What is JVM?**
JVM (Java Virtual Machine) is an abstract machine that executes Java bytecode. It makes Java
platform-independent.

**Q2. Difference between JDK, JRE, and JVM?**


- JDK: Includes compiler (javac), debugger, JRE, and development tools. Used for coding and
compiling.
- JRE: Includes JVM + libraries to run Java code. No compiler.
- JVM: Executes bytecode, manages memory, and provides runtime environment.

**Q3. Simple Java program:**


public class Hello {
public static void main(String[] args) {
System.out.println("Hello World");
}
}

**Q4. Data types in Java?**


- Primitive: byte, short, int, long, float, double, char, boolean
- Non-primitive: String, Arrays, Classes, Objects

---

2. Java Basics (loops, conditions)


**Q5. Check if a number is even or odd.**
public class EvenOdd {
public static void main(String[] args) {
int num = 5;
if (num % 2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
}
}

**Q6. Print numbers from 1 to 10.**


for (int i = 1; i <= 10; i++) {
System.out.print(i + " ");
}

**Q7. Factorial of a number.**


int n = 5, fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
System.out.println("Factorial: " + fact);

---

3. Arrays
**Q8. Find max element in array.**
int[] arr = {10, 20, 5, 7, 30};
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) max = arr[i];
}
System.out.println("Max: " + max);

**Q9. Reverse an array.**


int[] arr = {1, 2, 3, 4, 5};
for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

**Q10. Insert element in array.**


int[] arr = new int[6];
arr[0]=1; arr[1]=2; arr[2]=3; arr[3]=4; arr[4]=5;
int pos = 2, val = 99;
for (int i = 5; i > pos; i--) {
arr[i] = arr[i-1];
}
arr[pos] = val;

---
4. Strings
**Q11. Count vowels.**
String str = "Hello World";
int count = 0;
for (int i = 0; i < str.length(); i++) {
char ch = Character.toLowerCase(str.charAt(i));
if ("aeiou".indexOf(ch) != -1) count++;
}
System.out.println("Vowels: " + count);

**Q12. Palindrome check.**


String str = "madam";
String rev = "";
for (int i = str.length() - 1; i >= 0; i--) {
rev += str.charAt(i);
}
System.out.println(str.equals(rev) ? "Palindrome" : "Not Palindrome");

**Q13. Substring.**
String str = "Programming";
System.out.println(str.substring(2, 6)); // ogram

---

5. Searching & Sorting


**Q14. Linear Search.**
int[] arr = {10, 20, 30, 40, 50};
int key = 30, pos = -1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == key) { pos = i; break; }
}
System.out.println(pos == -1 ? "Not Found" : "Found at " + pos);

**Q15. Binary Search.**


int[] arr = {10, 20, 30, 40, 50};
int key = 40, low = 0, high = arr.length - 1;
boolean found = false;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key) { found = true; break; }
else if (arr[mid] < key) low = mid + 1;
else high = mid - 1;
}
System.out.println(found ? "Found" : "Not Found");

**Q16. Bubble Sort.**


int[] arr = {5, 1, 4, 2, 8};
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}

---

6. Problem Solving
**Q17. Fibonacci series.**
int a = 0, b = 1;
System.out.print(a + " " + b);
for (int i = 2; i < 10; i++) {
int c = a + b;
System.out.print(" " + c);
a = b;
b = c;
}

**Q18. Sum of digits.**


int num = 1234, sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
System.out.println("Sum: " + sum);

**Q19. Prime numbers up to 50.**


for (int n = 2; n <= 50; n++) {
boolean prime = true;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) { prime = false; break; }
}
if (prime) System.out.print(n + " ");
}

**Q20. Armstrong number.**


int num = 153, sum = 0, temp = num;
while (temp > 0) {
int d = temp % 10;
sum += d*d*d;
temp /= 10;
}
System.out.println(num == sum ? "Armstrong" : "Not Armstrong");

You might also like