Tricky Java Questions (Pattern Based)(10th class)
Tricky Java Questions (Pattern Based)
1(i)
What will be the output of the following code?
System.out.println(10 + 20 + "Java" + 30 +
40);
a) 3030Java
b) 30Java3040
c) 30Java70
d) Java3040
✅ Answer:
b) 30Java3040
📘 Topic: Operators
+ String Concatenation
💡 Reason: 10+20=30
(int addition), then "Java" makes the rest concatenation → 30Java3040.
1(ii)
Which of the following is a valid identifier in Java?
a) 1stValue
b) _result
c) new
d) a-b
✅ Answer:
b) _result
📘 Topic: Identifiers
& Keywords
💡 Reason:
Identifiers can start with _ or letter, but not digits or operators. new is
reserved keyword.
1(iii)
What will the following code print?
char ch = 'A';
System.out.println((int)(ch + 1));
a) 65
b) 66
c) A1
d) B
✅ Answer:
b) 66
📘 Topic: Type
Casting + ASCII
💡 Reason: 'A'
= 65, so 65+1 = 66.
1(iv)
Which of the following statements about switch in Java is true?
a) switch can work with float
b) switch can work with String (since Java 7)
c) switch can check ranges like case >5:
d) switch can work only with boolean
✅ Answer:
b) switch can work with String (since Java 7)
📘 Topic: Decision
Making (switch)
💡 Reason: switch
does not support float or ranges, and boolean is not allowed. String is allowed
from Java 7.
1(v)
What is the result of:
System.out.println(15/4 + " " + 15.0/4);
a) 3 3.0
b) 3 3.75
c) 3.0 3.75
d) 3.75 3.75
✅ Answer:
b) 3 3.75
📘 Topic: Integer
Division & Floating Division
💡 Reason: 15/4
= 3 (integer division). 15.0/4 = 3.75.
1(vi)
Which Math function gives the smallest integer greater than or equal to -3.8?
a) Math.floor(-3.8)
b) Math.ceil(-3.8)
c) Math.round(-3.8)
d) Math.abs(-3.8)
✅ Answer:
b) Math.ceil(-3.8)
📘 Topic: Math
Class
💡 Reason: ceil
moves up → nearest integer ≥ -3.8 is -3.
1(vii)
What will be the output of:
String s = "Hello";
System.out.println(s.substring(1,4));
a) Hel
b) Hell
c) ell
d) llo
✅ Answer:
c) ell
📘 Topic: String
Class Methods
💡 Reason: substring(start,
end) includes start=1 (‘e’) but excludes end=4.
1(viii)
What is the value of x after executing:
int x = 5;
x += x++ + ++x;
a) 16
b) 17
c) 18
d) 19
✅ Answer:
b) 17
📘 Topic: Operators
(Increment + Assignment)
💡 Reason:
- Initially
x=5
- x++
= 5 (then x=6)
- ++x
= 7 (x=7)
- So
x += 5+7 → 7+12=19 but since x=7 already, result = 17.
1(ix)
Which constructor call is valid for the following class?
class Test {
Test(int a,
double b) {}
}
a) new Test(10,20);
b) new Test(10.5,20);
c) new Test(10,20.5);
d) new Test();
✅ Answer:
c) new Test(10,20.5);
📘 Topic: Constructors
& Type Matching
💡 Reason:
First parameter must be int, second must be double.
1(x)
Given:
int arr[][] = {{1,2,3},{4,5,6}};
System.out.println(arr[1][2]);
What will it print?
a) 2
b) 3
c) 5
d) 6
✅ Answer:
d) 6
📘 Topic: 2D
Arrays in Java
💡 Reason: arr[1]
→ second row {4,5,6}. Index 2 = 6.
✨
These are tricky but exam-friendly and strictly follow the pattern:
- Question
- Options
- Answer
- Reason
- Topic
Super 👍
Let’s build a full Section A with 20 tricky MCQs in Java, exactly in the
pattern we fixed:
SECTION A – (20 × 1 = 20 Marks)
1(i)
What will be the output?
System.out.println(5 + 5 + "5" + 5);
a) 1055
b) 555
c) 155
d) 55
✅ Answer:
a) 1055
📘 Topic: String
Concatenation & Operators
💡 5+5=10 then "5"
→ 105 then +5 → 1055.
1(ii)
Which of the following is not a valid keyword in Java?
a) goto
b) static
c) switch
d) include
✅ Answer:
d) include
📘 Topic: Keywords
in Java
💡 goto is reserved
but unused, include belongs to C/C++.
1(iii)
The default value of an uninitialized boolean instance variable is:
a) true
b) false
c) 0
d) null
✅ Answer:
b) false
📘 Topic: Data
Types & Variables
1(iv)
Which of the following data types can store the largest value?
a) int
b) long
c) float
d) double
✅ Answer:
d) double
📘 Topic: Data
Types in Java
1(v)
What will be the output?
System.out.println(7/2*2.0);
a) 7.0
b) 6.0
c) 7
d) 6
✅ Answer:
b) 6.0
📘 Topic: Type
Casting & Division
💡 7/2=3 (int
division), then 3*2.0 = 6.0.
1(vi)
Which of the following is true for switch?
a) Supports float case labels
b) Case labels must be unique
c) Default must always be last
d) Break is optional but mandatory
✅ Answer:
b) Case labels must be unique
📘 Topic: Decision
Making
1(vii)
What will be the output?
char c = 'B';
System.out.println((int)c);
a) 65
b) 66
c) 67
d) 68
✅ Answer:
b) 66
📘 Topic: Characters
& ASCII
1(viii)
Which method returns the length of a string?
a) size()
b) length
c) length()
d) getSize()
✅ Answer:
c) length()
📘 Topic: String
Class Methods
1(ix)
What is printed?
System.out.println("Hello".substring(1,4));
a) Hel
b) ell
c) llo
d) Hell
✅ Answer:
b) ell
📘 Topic: Substring
in Strings
1(x)
Which of the following loops will execute exactly once, even if the
condition is false?
a) for loop
b) while loop
c) do…while loop
d) switch loop
✅ Answer:
c) do…while loop
📘 Topic: Loops
in Java
1(xi)
What is the output?
int x = 5;
System.out.println(x++ + ++x);
a) 10
b) 11
c) 12
d) 13
✅ Answer:
d) 13
📘 Topic: Increment
Operators
💡 x++=5, then ++x=7,
so 5+7=12. But after x=6, ++x=7, total 12? Wait check: x=5 → x++ =5, x=6 →
++x=7 → sum=12 ✅
Correction → Answer is c) 12.
1(xii)
What is the output of:
System.out.println(Math.floor(-2.1));
a) -2.0
b) -3.0
c) -1.0
d) 2.0
✅ Answer:
b) -3.0
📘 Topic: Math
Class Methods
1(xiii)
Which operator is used for bitwise AND in Java?
a) &&
b) &
c) |
d) ^
✅ Answer:
b) &
📘 Topic: Operators
(Bitwise)
1(xiv)
Which of these is a wrapper class in Java?
a) int
b) float
c) Integer
d) char
✅ Answer:
c) Integer
📘 Topic: Wrapper
Classes
1(xv)
What is the output?
int a = 2, b = 3;
System.out.println(Math.pow(a,b));
a) 6
b) 8.0
c) 9
d) 8
✅ Answer:
b) 8.0
📘 Topic: Math
Class – pow()
1(xvi)
Which exception occurs if you access an invalid array index?
a) NullPointerException
b) ArithmeticException
c) ArrayIndexOutOfBoundsException
d) NumberFormatException
✅ Answer:
c) ArrayIndexOutOfBoundsException
📘 Topic: Exceptions
in Java
1(xvii)
What is the output?
System.out.println("Java".compareTo("java"));
a) 0
b) Negative number
c) Positive number
d) Error
✅ Answer:
b) Negative number
📘 Topic: String
compareTo()
💡 Capital letters
< small letters in Unicode.
1(xviii)
Which of the following is a valid constructor definition?
a) void Test() {}
b) Test() {}
c) public void Test() {}
d) int Test() {}
✅ Answer:
b) Test() {}
📘 Topic: Constructors
💡 Constructors
don’t have a return type.
1(xix)
Given:
int arr[][] = {{1,2,3},{4,5,6}};
System.out.println(arr[0][2]);
What is the output?
a) 1
b) 2
c) 3
d) 6
✅ Answer:
c) 3
📘 Topic: 2D
Arrays
1(xx)
Which loop will run exactly 5 times?
a) for(int i=1; i<=5; i++)
b) for(int i=0; i<5; i++)
c) Both a & b
d) while(true)
✅ Answer:
c) Both a & b
📘 Topic: Loops
in Java
Java – Question 2 (New Set)
2(i) Rewrite using single if statement
if(ch == 'y')
System.out.println("YES");
else if(ch == 'Y')
System.out.println("YES");
✅ Answer:
if(ch == 'y' || ch == 'Y')
System.out.println("YES");
Explanation:
Used logical OR (||) to combine both conditions.
2(ii) Expression Evaluation
Evaluate when a=5, b=2:
a+= ++b + a-- - b++;
System.out.println("a= " + a);
System.out.println("b= " + b);
✅ Answer:
- Step
1: a = 5, b = 2
- Expression:
a = a + (++b + a-- - b++)
- ++b
= 3, so a = 5 + (3 + 5 - 3)
- a--
= 5 (then a = 4), b++ = 3 (then b = 4)
- So
a = 5 + 5 = 10
- Final
values → a = 10, b = 4
2(iii) Error Identification
float x = 3.4;
switch(x) {
case 3:
System.out.println("Three"); break;
case 4:
System.out.println("Four");
}
✅ Answer:
Error → switch does not allow float.
Corrected Code:
int x = 3;
switch(x) {
case 3:
System.out.println("Three"); break;
case 4:
System.out.println("Four");
}
2(iv) Java Expression Writing
Write Java expression for:
p2+q2\sqrt{p^2 + q^2}
✅ Answer:
Math.sqrt((p * p) + (q * q));
2(v) Loop Execution & Output
int n = 5;
while(n < 8){
System.out.println(n * n);
n++;
}
✅ Answer:
- Loop
executes 3 times (n=5,6,7).
- Output:
25
36
49
2(vi) String Methods
String s1 = "Hello", s2 = "HELLO";
System.out.println(s1.equals(s2));
System.out.println(s1.equalsIgnoreCase(s2));
✅ Answer:
false
true
2(vii) Character & ASCII Conversion
char c = 'd';
int val = (int)c + 5;
System.out.println((char)val + "\t" + c);
✅ Answer:
- 'd'
= ASCII 100
- 100
+ 5 = 105 → 'i'
- Output:
i d
2(viii) Error Type & Correction
String num = "100";
int x = Integer.parseInt(num);
double y = Math.sqrt(num);
System.out.println(y);
✅ Answer:
- Error:
Passing String directly in Math.sqrt() → compile-time error (syntax
error).
- Corrected
Code:
String num = "100";
int x = Integer.parseInt(num);
double y = Math.sqrt(x);
System.out.println(y);
Output: 10.0
2(ix) Constructors
class Student {
String name;
int age;
Student() {
name =
"Unknown";
age = 0;
}
Student(String n, int a) {
name = n;
age = a;
}
}
✅ Answer:
- Default
constructor → Student()
- Parameterized
constructor → Student(String n, int a)
2(x) 2D Array
int arr[][] = { {1,2,3}, {4,5,6}, {7,8,9} };
(a) Position of 8
(b) Value of arr[0][2] + arr[2][0]
✅ Answer:
(a) arr[2][1]
(b) 3 + 7 = 10
1. Armstrong Twins
👉
Definition: A number is Armstrong if sum of cubes of its digits = number
itself.
Example:
153 → 13+53+33=1+125+27=1531^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153 ✅
370 → 33+73+03=27+343+0=3703^3 + 7^3 + 0^3 = 27 + 343 + 0 = 370 ✅
📌 Hence, 153 and
370 are Armstrong Twins.
class ArmstrongTwins {
public
static void main(String args[]) {
int a =
153, b = 370;
int temp
= a, sum = 0;
while
(temp > 0) {
int
d = temp % 10;
sum
+= d * d * d;
temp
/= 10;
}
boolean
first = (sum == a);
temp =
b; sum = 0;
while
(temp > 0) {
int
d = temp % 10;
sum
+= d * d * d;
temp
/= 10;
}
boolean
second = (sum == b);
if
(first && second)
System.out.println(a + " and " + b + " are Armstrong
Twins");
else
System.out.println("Not Armstrong Twins");
}
}
2. Digit Rotation
👉
Rotate digits of a number left by one place.
Example:
Number = 12345
Step 1: Remove first digit → 2345
Step 2: Add removed digit at last → 23451
📌 Rotated result = 23451
class DigitRotation {
public
static void main(String args[]) {
String
num = "12345";
String
rotated = num.substring(1) + num.charAt(0);
System.out.println("Rotated Number = " + rotated);
}
}
3. Strong Number
👉
Sum of factorial of digits = number
Example:
145 → 1!+4!+5!1! + 4! + 5!
= 1+24+120=1451 + 24 + 120 = 145 ✅
📌 Hence, 145 is a Strong
Number.
class StrongNumber {
public
static void main(String args[]) {
int n =
145, temp = n, sum = 0;
while
(temp > 0) {
int
d = temp % 10;
int
f = 1;
for
(int i = 1; i <= d; i++) {
f *= i;
}
sum
+= f;
temp
/= 10;
}
if (sum
== n)
System.out.println(n
+ " is Strong Number");
else
System.out.println(n + " is not Strong Number");
}
}
4. Special Sum Number
👉
Sum of squares of digits + sum of digits = number
Example:
59 → Digits = 5, 9
Squares = 52+92=25+81=1065^2 + 9^2 = 25 + 81 = 106
Sum of digits = 5+9=145 + 9 = 14
Total = 106+14=120106 + 14 = 120
📌 So, 59 → 120
(Special Sum Result).
class SpecialSumNumber {
public
static void main(String args[]) {
int n =
59, temp = n, sumSq = 0, sumD = 0;
while
(temp > 0) {
int
d = temp % 10;
sumSq += d * d;
sumD
+= d;
temp
/= 10;
}
int
result = sumSq + sumD;
System.out.println("Special Sum = " + result);
}
}
5. Harshad–Palindrome
👉
Harshad = divisible by sum of digits; Palindrome = same backward.
Example:
171 → Digits = 1, 7, 1
Sum = 1+7+1=91 + 7 + 1 = 9
171÷9=19171 ÷ 9 = 19 ✅
Harshad
Also, Reverse of 171 = 171 ✅
Palindrome
📌 Hence, 171 is
both Harshad & Palindrome.
class HarshadPalindrome {
public
static void main(String args[]) {
int n =
171, temp = n, sum = 0;
while
(temp > 0) {
sum
+= temp % 10;
temp
/= 10;
}
boolean
harshad = (n % sum == 0);
String s
= Integer.toString(n);
String
rev = new StringBuilder(s).reverse().toString();
boolean
palindrome = s.equals(rev);
if
(harshad && palindrome)
System.out.println(n + " is Harshad & Palindrome");
else
System.out.println(n + " is not Harshad & Palindrome");
}
}
6. Digital Root
👉
Repeated sum of digits until single digit.
Example:
9875 → 9+8+7+5=299+8+7+5 = 29
29 → 2+9=112+9 = 11
11 → 1+1=21+1 = 2
📌 Digital root = 2
class DigitalRoot {
public
static void main(String args[]) {
int n =
9875;
while (n
> 9) {
int
sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
n =
sum;
}
System.out.println("Digital Root = " + n);
}
}
7. Gapful Number
👉
At least 3 digits, divisible by first+last digit number.
Example:
192 → First digit = 1, Last digit = 2 → 12
Check divisibility: 192÷12=16192 ÷ 12 = 16 ✅
📌 Hence, 192 is Gapful.
class GapfulNumber {
public
static void main(String args[]) {
String
num = "192";
int n =
Integer.parseInt(num);
int
divisor = Integer.parseInt("" + num.charAt(0) +
num.charAt(num.length() - 1));
if (n %
divisor == 0)
System.out.println(n + " is Gapful Number");
else
System.out.println(n + " is not Gapful Number");
}
}
8. Automorphic Number
👉
Square of number ends with the number itself.
Example:
76² = 5776 → ends with 76 ✅
📌 So, 76 is Automorphic.
class Automorphic {
public
static void main(String args[]) {
int n =
76;
long sq
= (long) n * n;
if
(String.valueOf(sq).endsWith(String.valueOf(n)))
System.out.println(n + " is Automorphic Number");
else
System.out.println(n + " is not Automorphic Number");
}
}
Comments
Post a Comment