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

Java DSA Basics for 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)
21 views5 pages

Java DSA Basics for 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

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) {
[Link]("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) {
[Link]("Even");
} else {
[Link]("Odd");
}
}
}

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


for (int i = 1; i <= 10; i++) {
[Link](i + " ");
}

**Q7. Factorial of a number.**


int n = 5, fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
[Link]("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 < [Link]; i++) {
if (arr[i] > max) max = arr[i];
}
[Link]("Max: " + max);

**Q9. Reverse an array.**


int[] arr = {1, 2, 3, 4, 5};
for (int i = 0, j = [Link] - 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 < [Link](); i++) {
char ch = [Link]([Link](i));
if ("aeiou".indexOf(ch) != -1) count++;
}
[Link]("Vowels: " + count);

**Q12. Palindrome check.**


String str = "madam";
String rev = "";
for (int i = [Link]() - 1; i >= 0; i--) {
rev += [Link](i);
}
[Link]([Link](rev) ? "Palindrome" : "Not Palindrome");

**Q13. Substring.**
String str = "Programming";
[Link]([Link](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 < [Link]; i++) {
if (arr[i] == key) { pos = i; break; }
}
[Link](pos == -1 ? "Not Found" : "Found at " + pos);

**Q15. Binary Search.**


int[] arr = {10, 20, 30, 40, 50};
int key = 40, low = 0, high = [Link] - 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;
}
[Link](found ? "Found" : "Not Found");

**Q16. Bubble Sort.**


int[] arr = {5, 1, 4, 2, 8};
for (int i = 0; i < [Link] - 1; i++) {
for (int j = 0; j < [Link] - 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;
[Link](a + " " + b);
for (int i = 2; i < 10; i++) {
int c = a + b;
[Link](" " + c);
a = b;
b = c;
}

**Q18. Sum of digits.**


int num = 1234, sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
[Link]("Sum: " + sum);

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


for (int n = 2; n <= 50; n++) {
boolean prime = true;
for (int i = 2; i <= [Link](n); i++) {
if (n % i == 0) { prime = false; break; }
}
if (prime) [Link](n + " ");
}

**Q20. Armstrong number.**


int num = 153, sum = 0, temp = num;
while (temp > 0) {
int d = temp % 10;
sum += d*d*d;
temp /= 10;
}
[Link](num == sum ? "Armstrong" : "Not Armstrong");

Common questions

Powered by AI

Java's platform independence, enabled by the JVM executing bytecode rather than machine-specific code, allows developers to write code once and execute it on any system with a JVM. This feature significantly accelerates software development and deployment by reducing the need for rewriting and debugging on different platforms, promoting cross-platform compatibility, and allowing smoother program transportability from development to testing, and finally to production environments .

Libraries in the JRE provide a set of pre-written classes and methods extensively used in Java programs, such as data structures, I/O functionality, networking, and GUI components. These libraries extend the JVM's capabilities by facilitating the interaction between Java bytecode and the host operating system, allowing tasks to be performed more efficiently and reducing the need for developers to write these from scratch, thereby ensuring the smooth execution of Java applications .

Bubble Sort compares adjacent elements, swapping them as necessary until no more swaps are needed, with a worst-case time complexity of O(n^2). It is less efficient than algorithms like Quick Sort (O(n log n) average time) and Merge Sort (also O(n log n) time), which perform better on average and in large datasets by dividing and conquering the problem space efficiently. Bubble Sort is typically used for educational purposes or when the dataset size is very small, due to its simplicity .

JVM (Java Virtual Machine) is an abstract machine that executes Java bytecode, making Java platform-independent by allowing the same code to run on any machine. JRE (Java Runtime Environment) includes the JVM and libraries needed to run Java applications, but it doesn't have development tools like a compiler. JDK (Java Development Kit) is a full-featured software development kit that includes the JRE along with tools such as the Java compiler, debugger, and other development utilities .

The Fibonacci series is generated by starting with two initial numbers, 0 and 1, and then iteratively calculating the next numbers as the sum of the last two. This sequence is used in dynamic programming, search algorithms, and data structures due to its recursive properties, reflecting optimization potential in calculations. In real-world scenarios, it's applied in financial models for growth prediction, biology for growth patterns in nature, and computer algorithms for improving calculation efficiencies .

Loops and conditions are fundamental control structures that direct the flow of a program, enabling repetitive tasks and decision-making processes. In Java, they allow programs to handle dynamic inputs and perform tasks efficiently without rewriting code blocks. For instance, loops allow iteration over data structures for processing, while conditions tailor the program's execution path based on Boolean logic. This flexibility and efficiency make them essential for crafting versatile and robust applications .

Understanding primitive and non-primitive data types is crucial because they dictate how data is stored, manipulated, and accessed in Java. Primitive types are predefined in Java and hold pure data values, taking up a fixed amount of memory, which results in efficiency but also limitations like not supporting methods. Non-primitive types, such as Strings and Arrays, reference objects and can store additional methods and information, providing flexibility and allowing dynamic data handling .

Linear search examines each element in the array sequentially until it finds the target element, or it checks all elements without finding it, in which case it concludes the element is not present. Its strength lies in its simplicity and ease of implementation, especially on unsorted datasets. However, it is inefficient for large arrays due to its O(n) time complexity. In contrast, binary search is much faster because it divides the search interval in half each step, only applicable to sorted arrays, operating in O(log n) time. Thus, binary search is typically preferred for efficiency in sorted arrays .

To reverse an array in Java, one can swap the elements starting from both ends of the array towards the center until the middle is reached. This procedure involves a loop that iterates n/2 times if n is the length of the array, meaning the time complexity is O(n), which is efficient for most applications. This straightforward approach effectively flips the array in place without requiring additional memory allocation for another array .

An Armstrong number is determined by taking each of the digits of the number, raising them to a power equal to the number of digits, and summing these values. If the sum equals the original number, it is an Armstrong number. Not every number qualifies because this condition is stringent; only numbers whose digit manipulations under this method reproduce the original number fulfill it, which usually limits qualifying numbers to smaller magnitudes due to their exponential sum potential .

You might also like