0% found this document useful (0 votes)
4 views2 pages

Java Quiz

The document contains a Java quiz with various coding problems and examples. It includes loops, conditionals, recursion, and string manipulation tasks. Each section presents a different programming challenge for the reader to solve.

Uploaded by

adrija1704
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)
4 views2 pages

Java Quiz

The document contains a Java quiz with various coding problems and examples. It includes loops, conditionals, recursion, and string manipulation tasks. Each section presents a different programming challenge for the reader to solve.

Uploaded by

adrija1704
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/ 2

JAVA QUIZ

1. int a = 1; 4.String str = "JavaProgramming";


for (int i = 1; i <= 3; i++) String result = "";
{ for (int i = 0; i < str.length(); i++) {
for (int j = 1; j <= i; j++) if (i % 2 == 0) result += str.charAt(i); else result += "*";
{ }
if ((i + j) % 2 == 0) System.out.println(result);
a += 2; 5.String str = "racecar";
else a -= 1; String rev = "";
System.out.print(a + " "); for (int i = str.length() - 1; i >= 0; i--)
} {
System.out.println(); rev += str.charAt(i);
} }
2. int[] arr = {2, 5, 8, 11, 14}; System.out.println(str.equals(rev));
int sum = 0; 6.int x = 5;
for (int i = 0; i < arr.length; i++) { for (int i = 1; i <= x; i++)
if (arr[i] % 2 == 0) {
{ for (int j = 1; j <= i; j++)
if (arr[i] % 3 == 0) {
sum += arr[i]; System.out.print((i + j) + " ");
else sum -= arr[i]; }
} System.out.println();
else sum += 1; }
} 7.
System.out.println(sum); String str = "java";
for (int i = 0; i < str.length(); i++)
3. public class RecursionExample {
{ System.out.print((char)(str.charAt(i) + 1));
public static void main(String[] args) { }
System.out.println(sum(5)); 8.public class Fibonacci {
} public static void main(String[] args) {
System.out.println(fib(5));
public static int sum(int n)
}
{
public static int fib(int n) {
if (n <= 1)
if (n <= 1)
return n;
return n;
return n + sum(n - 2);
return fib(n - 1) + fib(n - 2);
}
}
}
}
9.int count = 0; 15. int num = 4, product = 1;

for (int i = 1; i <= 3; i++) { for (int i = 1; i <= num; i++)

for (int j = i; j <= 3; j++) { {


for (int j = i; j <= num; j++)
count += i + j;
{
}}
product *= j % i == 0 ? i : j;
System.out.println(count);
}}
10.String str = "ICSEExam";
System.out.println(product);
String result = ""; 16.int a = 12, b = 3, result = 0;
for (int i = 0; i < str.length(); i++) { for (int i = 2; i <= 5; i++) {
if (i % 3 == 0) a -= b++ * i;
result += Character.toUpperCase(str.charAt(i)); result += a % b;

else b += i;
}
result += str.charAt(i);
System.out.println(result);
}
17.int sum = 0;
System.out.println(result);
for (int i = 1; i <= 5; i++)
11.int a = 3;
{
for (int i = 0; i < 4; i++) {
int temp = i;
a = (a % 2 == 0) ? a * 2 : a + 3; while (temp > 0) {
} sum += temp % 2 == 0 ? temp : -temp;
System.out.println(a); temp--;
12.int a = 0; }}

for (int i = 1; i <= 3; i++) { for (int j = 1; j <= i; j++) { System.out.println(sum);


18.int num = 4, total = 0;
a += i * j;
for (int i = 1; i <= num; i++) {
}
for (int j = 1; j <= i; j++) {
}
total += (i * j) % 2 == 0 ? j : i;
System.out.println(a);
}
13. }
int x = 1; System.out.println(total);
for (int i = 0; i < 4; i++) 19.int a = 6, b = 3;
{ for (int i = 2; i <= 5; i++) {

x += i % 2 == 0 ? i : -i; a = (a * b) % i;

} if (a % 2 == 0) {
b += 2;
System.out.println(x);
}
14.int a = 10, b = 20;
else {
boolean result = a++ < b-- && ++a > --b;
System.out.println(result); b -= 1;
}}
System.out.println(a + " " + b);
System.out.println(a + " " + b);

You might also like