Java Basics Practice
Getting started with Java: printing, reading input with Scanner, variables and simple programs.
1
Hello Java
Easy
Write a Java program that reads your name and prints "Hello, ! Welcome to Java."
Input Format
A single line containing the name.
Output Format
Hello, ! Welcome to Java.
Sample Test Cases
|
Sample Input 1
Alice |
Sample Output 1
Hello, Alice! Welcome to Java. |
|
Sample Input 2
Bob |
Sample Output 2
Hello, Bob! Welcome to Java. |
|
Sample Input 3
Java |
Sample Output 3
Hello, Java! Welcome to Java. |
|
Sample Input 4
X |
Sample Output 4
Hello, X! Welcome to Java. |
|
Sample Input 5
TechBridge24 |
Sample Output 5
Hello, TechBridge24! Welcome to Java. |
Constraints
1
Explanation
Use Scanner.nextLine() and System.out.println().
2
Sum of Two Integers
Easy
Read two integers A and B and print their sum.
Input Format
A single line with two integers A and B.
Output Format
A single integer: A + B.
Sample Test Cases
|
Sample Input 1
5 7 |
Sample Output 1
12 |
|
Sample Input 2
-3 8 |
Sample Output 2
5 |
|
Sample Input 3
0 0 |
Sample Output 3
0 |
|
Sample Input 4
1000000000 1000000000 |
Sample Output 4
2000000000 |
|
Sample Input 5
-10 3 |
Sample Output 5
-7 |
Constraints
-10^9
Explanation
Use int or long; System.out.println(a + b).
3
Product of Two Numbers
Easy
Read two integers and print their product.
Input Format
A single line with two integers A and B.
Output Format
A single integer: A * B.
Sample Test Cases
|
Sample Input 1
3 4 |
Sample Output 1
12 |
|
Sample Input 2
7 8 |
Sample Output 2
56 |
|
Sample Input 3
0 5 |
Sample Output 3
0 |
|
Sample Input 4
10 10 |
Sample Output 4
100 |
|
Sample Input 5
9999 1 |
Sample Output 5
9999 |
Constraints
1
Explanation
System.out.println(a * b).
4
Average of Three Numbers
Easy
Read three integers A, B, C and print their integer average (floor of the exact average).
Input Format
A single line with three integers A, B, C.
Output Format
A single integer: floor((A + B + C) / 3).
Sample Test Cases
|
Sample Input 1
10 20 30 |
Sample Output 1
20 |
|
Sample Input 2
1 2 3 |
Sample Output 2
2 |
|
Sample Input 3
1 1 1 |
Sample Output 3
1 |
|
Sample Input 4
1 1 2 |
Sample Output 4
1 |
|
Sample Input 5
100 200 300 |
Sample Output 5
200 |
Constraints
1
Explanation
Sum them and divide by 3 using integer division.
5
Print Your Details
Easy
Read your name, age and city on three separate lines, then print them in the format "Name: , Age: , City: ".
Input Format
Three lines: name, age, city.
Output Format
Name: , Age: , City:
Sample Test Cases
|
Sample Input 1
Alice 20 Bangalore |
Sample Output 1
Name: Alice, Age: 20, City: Bangalore |
|
Sample Input 2
Bob 25 Mumbai |
Sample Output 2
Name: Bob, Age: 25, City: Mumbai |
|
Sample Input 3
A 1 X |
Sample Output 3
Name: A, Age: 1, City: X |
|
Sample Input 4
Carol 30 Delhi |
Sample Output 4
Name: Carol, Age: 30, City: Delhi |
Constraints
1
Explanation
Read with nextLine() after nextInt() consumes the newline.
1
Swap Two Integers
Medium
Read two integers A and B and print them swapped: B followed by A, on one line.
Input Format
A single line with two integers A and B.
Output Format
A single line: B then A, space-separated.
Sample Test Cases
|
Sample Input 1
5 10 |
Sample Output 1
10 5 |
|
Sample Input 2
1 2 |
Sample Output 2
2 1 |
|
Sample Input 3
100 1 |
Sample Output 3
1 100 |
|
Sample Input 4
7 7 |
Sample Output 4
7 7 |
|
Sample Input 5
-3 4 |
Sample Output 5
4 -3 |
Constraints
-10^9
Explanation
Use a temp variable, or print b and a directly.
2
Find Quotient and Remainder
Medium
Read two positive integers A and B and print their integer quotient and remainder (A / B and A % B) on one line.
Input Format
A single line with two integers A and B.
Output Format
Two integers: quotient and remainder, space-separated.
Sample Test Cases
|
Sample Input 1
17 5 |
Sample Output 1
3 2 |
|
Sample Input 2
100 10 |
Sample Output 2
10 0 |
|
Sample Input 3
1 1 |
Sample Output 3
1 0 |
|
Sample Input 4
7 3 |
Sample Output 4
2 1 |
|
Sample Input 5
1000000000 3 |
Sample Output 5
333333333 1 |
Constraints
1
Explanation
Use / for quotient and % for remainder.
3
Area and Perimeter of Rectangle
Medium
Read length L and breadth B of a rectangle and print its area and perimeter on one line.
Input Format
A single line with two integers L and B.
Output Format
Two integers: area (L*B) and perimeter (2*(L+B)), space-separated.
Sample Test Cases
|
Sample Input 1
4 5 |
Sample Output 1
20 18 |
|
Sample Input 2
10 10 |
Sample Output 2
100 40 |
|
Sample Input 3
1 1 |
Sample Output 3
1 4 |
|
Sample Input 4
1000000 1 |
Sample Output 4
1000000 2000002 |
|
Sample Input 5
3 7 |
Sample Output 5
21 20 |
Constraints
1
Explanation
area = L * B; perimeter = 2 * (L + B).
4
ASCII Value of a Character
Medium
Read a single character and print its ASCII value.
Input Format
A single character.
Output Format
A single integer: the ASCII value.
Sample Test Cases
|
Sample Input 1
A |
Sample Output 1
65 |
|
Sample Input 2
a |
Sample Output 2
97 |
|
Sample Input 3
Z |
Sample Output 3
90 |
|
Sample Input 4
0 |
Sample Output 4
48 |
|
Sample Input 5
~ |
Sample Output 5
126 |
Constraints
A printable ASCII character (32-126).
Explanation
Cast char to int: (int) ch.
5
Last Digit of a Number
Medium
Read a positive integer N and print its last digit.
Input Format
A single integer N.
Output Format
A single integer: the last digit of N.
Sample Test Cases
|
Sample Input 1
1234 |
Sample Output 1
4 |
|
Sample Input 2
90 |
Sample Output 2
0 |
|
Sample Input 3
1 |
Sample Output 3
1 |
|
Sample Input 4
1000000000 |
Sample Output 4
0 |
|
Sample Input 5
987654321 |
Sample Output 5
1 |
Constraints
1
Explanation
last digit = N % 10.
1
Reverse the Digits
Hard
Read a positive integer N and print it with its digits reversed. Leading zeros are dropped.
Input Format
A single integer N.
Output Format
A single integer: the reversed number.
Sample Test Cases
|
Sample Input 1
12345 |
Sample Output 1
54321 |
|
Sample Input 2
100 |
Sample Output 2
1 |
|
Sample Input 3
1 |
Sample Output 3
1 |
|
Sample Input 4
9009 |
Sample Output 4
9009 |
|
Sample Input 5
123456789 |
Sample Output 5
987654321 |
Constraints
1
Explanation
Repeatedly take N % 10 and build the reversed number.
2
Sum of Digits
Hard
Read a positive integer N and print the sum of its digits.
Input Format
A single integer N.
Output Format
A single integer: the sum of the digits of N.
Sample Test Cases
|
Sample Input 1
1234 |
Sample Output 1
10 |
|
Sample Input 2
999 |
Sample Output 2
27 |
|
Sample Input 3
0 |
Sample Output 3
0 |
|
Sample Input 4
1000000000 |
Sample Output 4
1 |
|
Sample Input 5
987654321 |
Sample Output 5
45 |
Constraints
1
Explanation
Convert to string and sum each char, or use % 10 and / 10.
3
Check Divisibility
Hard
Read two integers A and B. Print "Divisible" if A is divisible by B, otherwise "Not Divisible".
Input Format
A single line with two integers A and B.
Output Format
"Divisible" or "Not Divisible".
Sample Test Cases
|
Sample Input 1
10 5 |
Sample Output 1
Divisible |
|
Sample Input 2
10 3 |
Sample Output 2
Not Divisible |
|
Sample Input 3
0 7 |
Sample Output 3
Divisible |
|
Sample Input 4
-12 4 |
Sample Output 4
Divisible |
|
Sample Input 5
7 7 |
Sample Output 5
Divisible |
Constraints
-10^9
Explanation
A is divisible by B when A % B == 0. Note: A can be negative.
4
Fahrenheit to Celsius
Hard
Read a temperature in Fahrenheit (a whole number) and print it in Celsius. Celsius = (F - 32) * 5 / 9. The input is chosen so the result is a whole number.
Input Format
A single integer F.
Output Format
A single integer: the temperature in Celsius.
Sample Test Cases
|
Sample Input 1
212 |
Sample Output 1
100 |
|
Sample Input 2
32 |
Sample Output 2
0 |
|
Sample Input 3
-40 |
Sample Output 3
-40 |
|
Sample Input 4
50 |
Sample Output 4
10 |
|
Sample Input 5
104 |
Sample Output 5
40 |
Constraints
-100
Explanation
Use integer arithmetic: (F - 32) * 5 / 9.
5
Total Marks and Percentage
Hard
Read marks for 5 subjects (each out of 100) and print the total and the percentage (as an integer, rounded down).
Input Format
A single line with five integers, space-separated.
Output Format
Two integers: total marks and percentage (total / 5), space-separated.
Sample Test Cases
|
Sample Input 1
80 90 70 85 95 |
Sample Output 1
420 84 |
|
Sample Input 2
0 0 0 0 0 |
Sample Output 2
0 0 |
|
Sample Input 3
100 100 100 100 100 |
Sample Output 3
500 100 |
|
Sample Input 4
40 50 60 70 80 |
Sample Output 4
300 60 |
Constraints
0
Explanation
total = sum; percentage = total / 5 (integer division).
Competitive MCQs — Java Basics
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score
0/ 32
Q1
Which method is the entry point of a Java program?
Correct!
Wrong — correct answer is .
The JVM looks for public static void main(String[] args) to start execution.
Q2
What is the default value of an int variable declared as a field (not local)?
Correct!
Wrong — correct answer is .
Instance/static int fields default to 0; local ints have no default.
Q3
Which of these is a valid Java class declaration?
Correct!
Wrong — correct answer is .
The access modifier comes first: public class MyClass { }.
Q4
What is the output of this code?
java
1
System.out.println("5" + 2 + 3);Correct!
Wrong — correct answer is .
Left-to-right: "5" + 2 = "52" (string), then + 3 = "523".
Q5
What is the output of this code?
System.out.println("5" + 2 + 3);
System.out.println("5" + 2 + 3);
Correct!
Wrong — correct answer is .
Left-to-right: "5" + 2 = "52" (string), then + 3 = "523".
Q6
Which data type stores a single character?
Correct!
Wrong — correct answer is .
char stores one 16-bit Unicode character.
Q7
What does the JVM do?
Correct!
Wrong — correct answer is .
javac compiles source; the JVM runs the bytecode.
Q8
Which is the correct way to declare an integer variable x with value 10?
Correct!
Wrong — correct answer is .
Java uses type name = value; syntax.
Q9
What is the size of an int in Java?
Correct!
Wrong — correct answer is .
int is a 32-bit signed two's-complement integer.
Q10
What is the output of this code?
java
1
2
3
2
3
int a = 7;
int b = 2;
System.out.println(a / b);Correct!
Wrong — correct answer is .
Integer division of two ints truncates: 7 / 2 = 3.
Q11
What is the output of this code?
int a = 7;
int b = 2;
System.out.println(a / b);
int a = 7;
int b = 2;
System.out.println(a / b);
Correct!
Wrong — correct answer is .
Integer division of two ints truncates: 7 / 2 = 3.
Q12
Which keyword is used to create a constant in Java?
Correct!
Wrong — correct answer is .
final prevents reassignment (const is reserved but unused).
Q13
Which of these is a valid long literal in Java?
Correct!
Wrong — correct answer is .
The suffix L marks an integer literal as a long.
Q14
What is the default value of a boolean field?
Correct!
Wrong — correct answer is .
Instance/static boolean fields default to false.
Q15
What is the output of this code?
System.out.println(10 / 3);
System.out.println(10 / 3);
Correct!
Wrong — correct answer is .
Integer division truncates the fractional part: 10 / 3 = 3.
Q16
Which data type can store the largest whole numbers?
Correct!
Wrong — correct answer is .
long is 64-bit; int is 32-bit; short is 16-bit; byte is 8-bit.
Q17
What is the size of a char in Java?
Correct!
Wrong — correct answer is .
Java char is 16-bit Unicode (UTF-16 code unit).
Q18
What is the output of this code?
int x = 5;
System.out.println(x++);
int x = 5;
System.out.println(x++);
Correct!
Wrong — correct answer is .
x++ returns the current value 5, then increments x to 6.
Q19
Which keyword declares a class variable that belongs to the class itself?
Correct!
Wrong — correct answer is .
static members belong to the class rather than to instances.
Q20
What is the output of this code?
System.out.println("Java" == new String("Java"));
System.out.println("Java" == new String("Java"));
Correct!
Wrong — correct answer is .
== on objects compares references; the new String is a different object.
Q21
Which primitive type stores true/false?
Correct!
Wrong — correct answer is .
Java spells it boolean (lowercase); Boolean is the wrapper class.
Q22
What does the main method signature require?
Correct!
Wrong — correct answer is .
The JVM entry point is exactly public static void main(String[] args).
Q23
What is the output of this code?
System.out.println("A" + 1 + 2);
System.out.println("A" + 1 + 2);
Correct!
Wrong — correct answer is .
String concatenation is left-to-right: "A" + 1 = "A1", then + 2 = "A12".
Q24
What is the range of a byte in Java?
Correct!
Wrong — correct answer is .
byte is an 8-bit signed integer: -128 to 127.
Q25
Which is a valid variable name in Java?
Correct!
Wrong — correct answer is .
Identifiers may start with a letter, $, or underscore; class is a keyword.
Q26
What is the output of this code?
int a = 10;
double b = 3;
System.out.println(a / b);
int a = 10;
double b = 3;
System.out.println(a / b);
Correct!
Wrong — correct answer is .
Mixing int and double promotes to double, so division yields 3.333...
Q27
What does javac do?
Correct!
Wrong — correct answer is .
javac is the Java compiler; java runs the bytecode.
Q28
What is a package in Java?
Correct!
Wrong — correct answer is .
Packages organize classes and avoid name conflicts.
Q29
What is the output of this code?
System.out.println(2 + 3 * 2);
System.out.println(2 + 3 * 2);
Correct!
Wrong — correct answer is .
Multiplication binds first: 2 + (3 * 2) = 8.
Q30
Which of these is NOT a Java primitive type?
Correct!
Wrong — correct answer is .
String is a reference type; the others are primitives.
Q31
What is the wrapper class for int?
Correct!
Wrong — correct answer is .
Integer wraps an int for use where objects are required.
Q32
What is the output of this code?
System.out.println("Result: " + (5 + 3));
System.out.println("Result: " + (5 + 3));
Correct!
Wrong — correct answer is .
Parentheses force addition before string concatenation.