0% found this document useful (0 votes)
7 views11 pages

Java 50 Questions Answers

The document contains 50 Java coding questions along with their answers, covering a range of topics from basic syntax to data structures and algorithms. Each question is presented with a code snippet that demonstrates the solution. The questions include tasks like printing 'Hello World', checking for prime numbers, sorting arrays, and implementing data structures like stacks and queues.

Uploaded by

yashprataap4
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)
7 views11 pages

Java 50 Questions Answers

The document contains 50 Java coding questions along with their answers, covering a range of topics from basic syntax to data structures and algorithms. Each question is presented with a code snippet that demonstrates the solution. The questions include tasks like printing 'Hello World', checking for prime numbers, sorting arrays, and implementing data structures like stacks and queues.

Uploaded by

yashprataap4
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

50 Java Coding Questions & Answers

Q1. Print Hello World

public class Main {


public static void main(String[] args) {
[Link]("Hello World");
}
}

Q2. Swap two numbers without third variable

int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
[Link](a + " " + b);

Q3. Check even or odd

int n = 7;
[Link](n % 2 == 0 ? "Even" : "Odd");

Q4. Largest of three numbers

int a = 10, b = 20, c = 15;


int max = [Link](a, [Link](b, c));
[Link](max);

Q5. Check prime number

int n = 7;
boolean isPrime = n > 1;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) { isPrime = false; break; }
}
[Link](isPrime ? "Prime" : "Not Prime");

Q6. Fibonacci series (n terms)


int n = 10, a = 0, b = 1;
for (int i = 0; i < n; i++) {
[Link](a + " ");
int next = a + b;
a = b;
b = next;
}

Q7. Reverse a number

int n = 1234, rev = 0;


while (n != 0) {
rev = rev * 10 + (n % 10);
n /= 10;
}
[Link](rev);

Q8. Check palindrome number

int n = 121, original = n, rev = 0;


while (n != 0) {
rev = rev * 10 + (n % 10);
n /= 10;
}
[Link](original == rev ? "Palindrome" : "Not Palindrome");

Q9. Count digits in a number

int n = 12345, count = 0;


while (n != 0) { count++; n /= 10; }
[Link](count);

Q10. Sum of digits

int n = 1234, sum = 0;


while (n != 0) { sum += n % 10; n /= 10; }
[Link](sum);

Q11. Factorial of a number

int n = 5; long fact = 1;


for (int i = 2; i <= n; i++) fact *= i;
[Link](fact);
Q12. Multiplication table

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

Q13. Check leap year

int year = 2024;


boolean leap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
[Link](leap ? "Leap Year" : "Not Leap Year");

Q14. ASCII value of a character

char ch = 'A';
[Link]((int) ch);

Q15. Simple calculator using switch

int a = 10, b = 5; char op = '+';


switch (op) {
case '+': [Link](a + b); break;
case '-': [Link](a - b); break;
case '*': [Link](a * b); break;
case '/': [Link](b != 0 ? a / b : "Divide by zero"); break;
default: [Link]("Invalid operator");
}

Q16. GCD (HCF) of two numbers

int a = 36, b = 60;


while (b != 0) {
int t = b;
b = a % b;
a = t;
}
[Link](a);

Q17. LCM of two numbers


int a = 12, b = 18;
int x = a, y = b;
while (y != 0) {
int t = y;
y = x % y;
x = t;
}
int gcd = x;
int lcm = (a / gcd) * b;
[Link](lcm);

Q18. Count vowels and consonants in a string

String s = "Hello World";


int v = 0, c = 0;
for (char ch : [Link]().toCharArray()) {
if (ch >= 'a' && ch <= 'z') {
if ("aeiou".indexOf(ch) >= 0) v++; else c++;
}
}
[Link]("Vowels=" + v + ", Consonants=" + c);

Q19. Reverse a string

String s = "Java";
String rev = new StringBuilder(s).reverse().toString();
[Link](rev);

Q20. Check palindrome string

String s = "madam";
String rev = new StringBuilder(s).reverse().toString();
[Link]([Link](rev) ? "Palindrome" : "Not Palindrome");

Q21. Bubble sort an array

int[] arr = {5, 3, 8, 4, 2};


for (int i = 0; i < [Link] - 1; i++) {
for (int j = 0; j < [Link] - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int t = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = t;
}
}
}
[Link]([Link](arr));
Q22. Largest element in array

int[] arr = {1, 7, 3, 9, 5};


int max = arr[0];
for (int x : arr) if (x > max) max = x;
[Link](max);

Q23. Second largest element in array

int[] arr = {1, 7, 3, 9, 5};


int max = Integer.MIN_VALUE, second = Integer.MIN_VALUE;
for (int x : arr) {
if (x > max) { second = max; max = x; }
else if (x > second && x != max) { second = x; }
}
[Link](second);

Q24. Remove duplicates from array

int[] arr = {1, 2, 2, 3, 4, 4, 5};


[Link]<Integer> set = new [Link]<>();
for (int x : arr) [Link](x);
[Link](set);

Q25. Merge two arrays

int[] a = {1, 3, 5}, b = {2, 4, 6};


int[] c = new int[[Link] + [Link]];
[Link](a, 0, c, 0, [Link]);
[Link](b, 0, c, [Link], [Link]);
[Link]([Link](c));

Q26. Linear search

int[] arr = {2, 4, 6, 8};


int key = 6, idx = -1;
for (int i = 0; i < [Link]; i++) {
if (arr[i] == key) { idx = i; break; }
}
[Link](idx);
Q27. Binary search (sorted array)

int[] arr = {1, 3, 5, 7, 9};


int key = 7, l = 0, r = [Link] - 1, idx = -1;
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == key) { idx = m; break; }
else if (arr[m] < key) l = m + 1;
else r = m - 1;
}
[Link](idx);

Q28. Frequency of elements in array

int[] arr = {1, 2, 2, 3, 3, 3};


[Link]<Integer, Integer> map = new [Link]<>();
for (int x : arr) [Link](x, [Link](x, 0) + 1);
[Link](map);

Q29. Check Armstrong number (3-digit)

int n = 153, sum = 0, t = n;


while (t != 0) {
int d = t % 10;
sum += d * d * d;
t /= 10;
}
[Link](sum == n ? "Armstrong" : "Not Armstrong");

Q30. Print star pattern

int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) [Link]("*");
[Link]();
}

Q31. Rotate array left by k

int[] arr = {1,2,3,4,5};


int k = 2; k %= [Link];
int[] res = new int[[Link]];
for (int i = 0; i < [Link]; i++) {
res[i] = arr[(i + k) % [Link]];
}
[Link]([Link](res));

Q32. Find missing number (1..n)

int[] arr = {1,2,4,5}; int n = 5;


int sum = n * (n + 1) / 2;
for (int x : arr) sum -= x;
[Link](sum);

Q33. Move zeros to end

int[] arr = {0,1,0,3,12};


int idx = 0;
for (int x : arr) if (x != 0) arr[idx++] = x;
while (idx < [Link]) arr[idx++] = 0;
[Link]([Link](arr));

Q34. Find duplicate element in array

int[] arr = {1,2,3,2,4};


[Link]<Integer> seen = new [Link]<>();
for (int x : arr) {
if (![Link](x)) {
[Link](x);
break;
}
}

Q35. Intersection of two arrays

int[] a = {1,2,3,4}, b = {3,4,5,6};


[Link]<Integer> set = new [Link]<>();
for (int x : a) [Link](x);
[Link]<Integer> res = new [Link]<>();
for (int x : b) if ([Link](x)) [Link](x);
[Link](res);

Q36. Union of two arrays

int[] a = {1,2,3}, b = {3,4,5};


[Link]<Integer> set = new [Link]<>();
for (int x : a) [Link](x);
for (int x : b) [Link](x);
[Link](set);

Q37. Kadane's Algorithm (max subarray sum)

int[] arr = {-2,1,-3,4,-1,2,1,-5,4};


int maxSoFar = arr[0], curr = arr[0];
for (int i = 1; i < [Link]; i++) {
curr = [Link](arr[i], curr + arr[i]);
maxSoFar = [Link](maxSoFar, curr);
}
[Link](maxSoFar);

Q38. Longest word in a sentence

String s = "Java is a powerful programming language";


String[] words = [Link]("\\s+");
String longest = "";
for (String w : words) if ([Link]() > [Link]()) longest = w;
[Link](longest);

Q39. First non-repeating character

String s = "aabbcde";
int[] freq = new int[256];
for (char ch : [Link]()) freq[ch]++;
for (char ch : [Link]()) {
if (freq[ch] == 1) { [Link](ch); break; }
}

Q40. Check anagram strings

String a = "listen", b = "silent";


char[] x = [Link](), y = [Link]();
[Link](x);
[Link](y);
[Link]([Link](x, y));

Q41. Stack using array

class Stack {
int[] a; int top = -1;
Stack(int n){ a = new int[n]; }
void push(int x){ a[++top] = x; }
int pop(){ return a[top--]; }
}

Q42. Queue using array (circular)

class Queue {
int[] a; int f=0, r=0, size=0;
Queue(int n){ a=new int[n]; }
void offer(int x){ a[r]=x; r=(r+1)%[Link]; size++; }
int poll(){ int x=a[f]; f=(f+1)%[Link]; size--; return x; }
}

Q43. Reverse a linked list

class Node { int val; Node next; Node(int v){val=v;} }


Node reverse(Node head){
Node prev=null, curr=head;
while(curr!=null){
Node next=[Link];
[Link]=prev;
prev=curr;
curr=next;
}
return prev;
}

Q44. Detect cycle in linked list (Floyd)

boolean hasCycle(Node head){


Node slow=head, fast=head;
while(fast!=null && [Link]!=null){
slow=[Link];
fast=[Link];
if(slow==fast) return true;
}
return false;
}

Q45. Binary Search Tree (insert & inorder)

class BST {
class Node { int val; Node l,r; Node(int v){val=v;} }
Node root;
Node insert(Node n,int v){
if(n==null) return new Node(v);
if(v<[Link]) n.l=insert(n.l,v); else n.r=insert(n.r,v);
return n;
}
void inorder(Node n){
if(n==null) return;
inorder(n.l); [Link]([Link]+" "); inorder(n.r);
}
}

Q46. Level order traversal (BFS) of tree

void levelOrder(Node root){


[Link]<Node> q = new [Link]<>();
[Link](root);
while(![Link]()){
Node n = [Link]();
[Link]([Link]+" ");
if(n.l!=null) [Link](n.l);
if(n.r!=null) [Link](n.r);
}
}

Q47. DFS traversal (recursive)

void dfs(Node n){


if(n==null) return;
[Link]([Link]+" ");
dfs(n.l);
dfs(n.r);
}

Q48. Dijkstra (shortest path, adjacency list)

int[] dijkstra([Link]<int[]>[] g, int src){


int n=[Link];
int[] dist=new int[n];
[Link](dist, Integer.MAX_VALUE);
dist[src]=0;
[Link]<int[]> pq=new [Link]<>((a,b)->a[1]-b[1]);
[Link](new int[]{src,0});
while(![Link]()){
int[] cur=[Link]();
int u=cur[0], d=cur[1];
if(d!=dist[u]) continue;
for(int[] e:g[u]){
int v=e[0], w=e[1];
if(dist[v]>d+w){
dist[v]=d+w;
[Link](new int[]{v,dist[v]});
}
}
}
return dist;
}

Q49. Producer-Consumer (basic, synchronized)

class PC {
private final [Link]<Integer> q = new [Link]<>();
private final int cap = 5;
public synchronized void produce(int x) throws InterruptedException {
while([Link]()==cap) wait();
[Link](x); notifyAll();
}
public synchronized int consume() throws InterruptedException {
while([Link]()) wait();
int v=[Link](); notifyAll(); return v;
}
}

Q50. LRU Cache (LinkedHashMap)

class LRU<K,V> extends [Link]<K,V>{


private final int cap;
LRU(int cap){ super(16,0.75f,true); [Link]=cap; }
protected boolean removeEldestEntry([Link]<K,V> e){
return size()>cap;
}
}

You might also like