- Java Programs
- Java Programs - Home
- Short Questions/Answers
- Short Questions/Answers - Part 1
- Short Questions/Answers - Part 2
- Short Questions/Answers - Part 3
- Short Questions/Answers - Part 4
- Short Questions/Answers - Part 5
- Short Questions/Answers - Part 6
- Short Questions/Answers - Part 7
- Short Questions/Answers - Part 8
- Short Questions/Answers - Part 9
- Short Questions/Answers - Part 10
- Short Questions/Answers - Part 11
- Short Questions/Answers - Part 12
- Short Questions/Answers - Part 13
- Short Questions/Answers - Part 14
- Short Questions/Answers - Part 15
- Short Questions/Answers - Part 16
- Short Questions/Answers - Part 17
- Short Questions/Answers - Part 18
- Short Questions/Answers - Part 19
- Short Questions/Answers - Part 20
- Short Questions/Answers - Part 21
- Short Questions/Answers - Part 22
- Basic Input/Output & Data Types
- Beginner Level
- Print "Hello World"
- Print user's name
- Add two integers
- Swap two numbers (using temp)
- Find ASCII value of a character
- Multiply two floating values
- Convert integer to float and vice-versa
- Area of a circle
- Celsius to Fahrenheit conversion
- Read and display user input
- Intermediate Level
- Swap numbers (without temp)
- Compound interest calculator
- Simple calculator using user input
- Find quotient & remainder
- Check if number is positive, negative, or zero
- Check if a character is vowel or consonant
- Compute square & cube of a number
- Convert days to years + weeks + days
- Perimeter of rectangle
- Volume of a cylinder
- Advanced Level
- Menu-driven Basic Calculator using switch
- Evaluate mathematical expression with precedence
- Read full line input incl. spaces using Scanner
- Find result of power without Math.pow()
- Accept & process command line arguments
- Format output using printf() specifiers
- Currency conversion menu (USD, INR, EUR etc.)
- BMI calculator
- Unit converter (km-mile, cm-inch, etc.)
- EMI calculator
- Random number generator within range
- Generate unique invoice ID using timestamp
- Student Marks → Grade using nested switch
- Compute electricity bill based on slabs
- Salary slip generation with allowances
- Hotel bill calculator with discounts
- Data type range display using wrappers
- Interest comparison FD vs RD
- Leap year check with logical operators
- Convert lower ↔ upper case without built-in methods
- Input validation: numeric only
- Library due fine calculator
- Operators & Expressions
- Beginner Level
- Perform arithmetic operations (+, –, ×, ÷, %)
- Demonstrate increment and decrement operators
- Compare two numbers using relational operators
- Logical operator demonstration (AND, OR, NOT)
- Largest of two numbers using ternary operator
- Check if number is even or odd
- Sum of first N natural numbers
- Product of first N natural numbers
- Calculate square and cube of a number using *
- Demonstrate compound assignment operators (+=, -=, etc.)
- Intermediate Level
- Evaluate an expression with multiple operators (precedence)
- Convert seconds to hours, minutes & seconds
- Percentage & result using multi-expression formula
- Distance converter using arithmetic operators
- Temperature unit converter with precise formatting
- Demonstrate bitwise operators (&, |, ^, ~, <<, >>)
- Check if number is multiple of 3 or 5 using logical conditions
- Calculate profit or loss percentage
- Compute average of numbers using dynamic input
- Perimeter and area of geometric shapes (menu-driven)
- Advanced Level
- Solve quadratic equation for real & imaginary roots
- EMI, interest & monthly amortization calculator
- Electricity consumption bill using slabs
- Grade evaluation with combined logical expressions
- Loan eligibility check based on salary & CIBIL
- Multi-condition eligibility (job application screening)
- Age difference calculator using expressions
- Parse mathematical expression using Math functions
- Evaluate trigonometric expressions (sin, cos, tan)
- BMI category classification using chained ternary
- Discount calculation with multiple slabs
- Validate triangle using sum of angles
- Calculate Compound Annual Growth Rate (CAGR)
- Find leap year with combined operators
- Convert days into years + months + days logically
- Evaluate salary after tax based on slabs
- Compute & display MPG and fuel efficiency
- Compare two floats precisely (epsilon-based)
- Calculate Speed = Distance / Time + unit handling
- Bank interest vs inflation comparison program
- Demonstrate short-circuit behavior in logical operators
- Cricket run rate & required run rate calculator
- Number Based Programs
- Check Sum Program
- SUPERSPY Program
- EvenPal Program
- Duck Number Program
- EvenPal Program
- Student Range Program
- Tech Number Program
- Pronic Number
- Deviation Program
- Spy Number Program
- Sum of Prime Numbers Program
- Niven Number Program
- Menu Driven - Factors/Factorial Program
- Special Two-digit Number Program
- International Standard Book Number (ISBN) Program
- Piglatin Program
- Menu Driven - Composite Number / Smallest digit in Number Program
- Income Tax Calculation Program
- Menu Driven - Fibonacci / Sum of Digits Program
- STD (Subscriber Trunk Dialing) Codes Program
- Special Number Program
- Ticket Discount Program
- Menu Driven - Prime Number / Automorphic Number Program
- Electronic Shop Discount Program
- Menu driven - BUZZ Number / GCD Program
- Exam Results Program
- Tax Computation Program
- Menu Driven - Palindrome / Perfect Number Program
- Salary Calculation Program
- Maximum & Minimum Program
- Menu Driven - Temperature Program
- Square root of sum of squares of all elements - NORM
- Menu Driven Program
- Menu Driven Program
- Menu Driven Program - Pattern & Series
- Menu Driven Program - Generate Unicode, Pattern
- Menu Driven - Bank Deposit Program
- String Programs
- Super String Program
- String to Uppercase Program
- Gmail ID Validation Program
- Count the Vowels Program
- Print Number of Digits, Alphabets and Special Characters Program
- UpperCase Conversion Program
- Pair of Vowels Program
- Count Letters in a Sentence Program
- Proper Case Sentence Program
- Abbreviation Program
- Palindrome or Special Word Program
- Assign Full Path and File name Program
- Double Letter Sequences in a String Program
- New Word by replacing Vowels with the next Character Program
- Frequency of Characters in a String Program
- Longest Word Program
- Reverse Uppercase & Lowercase in String Program
- Count of a word in String Program
- Palindrome Program
- Class and Object Programs
- Class & Object Based Program - CloudStorage
- Class & Object Based Program - Bank
- Class & Object Based Program - Courier Service Program
- Class & Object Based Program - Eshop
- Class & Object Based Program - Student Stream Allocation
- Class & Object Based Program - Hotel Bill Calculation
- Class & Object Based Program - Cab Service
- Class & Object Based Program
- Class Overload Program
- Class & Object Based Program
- Class & Object Based Program - Railway Ticket
- Class & Object Based Program - Electric Bill
- Class & Object Based - Taxi Meter Program
- Class & Object - Book Fair Program
- Class & Object - Parking Lot Program
- Class & Object - Movie Magic Program
- Class & Object - FruitJuice Program
- Class & Object - Library Program
- Class & Object - Mobike Program
- Class & Object - Student Marks Program
- Overloading Programs
- Menu Driven Program - Overload Method
- Method Overloading Program
- Method Overloading Program
- Menu Driven Program - Overload Method
- Menu Driven Program - Overload Method
- Method Overloading - Pattern & Series
- Method Overloading
- Series Overload Program
- Function Overloading - Volume Program
- Function Overload - String Program
- Function Overloading - Sum of Series Program
- Function Overloading String Based Program
- Function Overloading - Area Program
- Function Overloading - Sum of Series Program
- Function Overloading - Polygon Program
- Function Overloading - Compare Integers, Characters, Strings Program
- Function Overloading - Volume Program
- Array Programs
- Binary Search Program
- Binary Search Program
- Sum of Row in Array Program
- Diagonal Array Program
- Selection Sort Program
- Selection Sort Program
- Sum of Even & Odd Elements in Array Program
- Bubble Sort Program
- Linear Search Program
- Sum of Single and Double Digit Number in Array Program
- Bubble Sort Program
- Student Range Program
- Binary Search Technique Program
- Double Dimensional Array - Diagonal Sum
- Arrange Student details using Selection Sort Method
- Sum of elements in Double Dimensional Array Program
- Sort Array Elements in Ascending Order using the Bubble Sort Technique Program
- Array Based Program - Largest Number, Smallest Number, Sum of Numbers Program
- Alphabet Sort - Selection Sort Techniques Program
- Largest & Smallest Integer in Array Program
- Sort Names in Array Program
- Two Different Arrays Program
- Array Based Student Result Program
- Arrange Names - Bubble Sort Technique Program
- Array Sort - Binary Search Program
- Descending Order Bubble Sort Program
- Descending Order - Selection Sort Program
- Binary Search Program
- Combine Two Arrays in Third Array Program
- Function Overloading - Number Calculation Program
- Sort String Bubble Sort Program
- Pattern Programs
- Menu Driven Program - Pattern
- Menu Driven - Pattern Program
- Pattern Program
- Menu Driven - Series Program
- Generate Triangle / Inverted Triangle Program
- Series Programs
- Sum of Series Program
- Sum of Series Program
- Menu Driven Program - Series Program
- Menu driven - Sum of Series Program
Java Programs
![]() Share with a Friend |
Short Questions/Answers - Java Programs
Question 1
Choose the correct answer to the questions from the given options.
(i)
Name the above structure:
(a) One dimensional array
(b) Two-dimensional array with 4 rows and 5 columns
(c) Three-dimensional array
(d) Two-dimensional array with 5 rows and 4 columns
(ii) “Java compiled code (bytecode) can run on all operating systems.”
– Name the feature.
(a) Robust and Secure
(b) Object-Oriented
(c) Platform-independent
(d) Multithreaded
(iii) The size of ‘\n’ is:
(a) 2 bytes
(b) 4 bytes
(c) 8 bytes
(d) 16 bytes
(iv) Identify the operator that gets the highest precedence while evaluating the given expression: a + b % c * d – e
(a) +
(b) %
(c) –
(d) *
(v) Which of the following is a valid Java keyword?
(a) If
(b) BOOLEAN
(c) static
(d) Switch
(vi) The output of the following code is:
System.out.println(Math.ceil(6.4) + Math.floor(-1-2));
(a) 3.0
(b) 4
(c) 3
(d) 4.0
(vii) Which of the following returns a String?
(a) length()
(b) charAt(int)
(c) replace(char, char)
(d) indexOf(String)
(viii) Which of the following is not true with regards to a switch statement?
(a) checks for an equality between the input and the case labels
(b) supports floating point constants
(c) break is used to exit from the switch block
(d) case labels are unique
(ix) Consider the array given below:
char ch[] = {‘A’, ‘E’, ‘I’, ‘O’, ‘U’};
Write the output of the following statement:
System.out.println(ch[0] * 2);
(a) 65
(b) 130
(c) ‘A’
(d) 0
(x) To execute a loop 10 times, which of the following is correct?
(a) for(int i = 11; i <= 30; i += 2)
(b) for(int i = 11; i <= 30; i += 3)
(c) for(int i = 11; i < 20; i++)
(d) for(int i = 11; i <= 21; i++)
(xi) A single dimensional array has 50 elements, which of the following is the correct statement to initialize the last element to 100?
(a) x[51] = 100;
(b) x[48] = 100;
(c) x[49] = 100;
(d) x[50] = 100;
(xii) Method prototype for the method compute which accepts two integer arguments and returns true/false.
(a) void compute(int a, int b)
(b) boolean compute(int a, int b)
(c) Boolean compute(int a, b)
(d) int compute(int a, int b)
(xiii) The statement that brings the control back to the calling method is:
(a) break
(b) System.exit(0)
(c) continue
(d) return
(xiv) The default value of a boolean variable is:
(a) False
(b) 0
(c) false
(d) True
(xv) The method to convert a lowercase character to uppercase is:
(a) String.toUpperCase()
(b) Character.isUppercase(char)
(c) Character.toUpperCase(char)
(d) toUpperCase()
(xvi) Assertion (A): Integer class can be used in the program without calling a package.
Reason (R): It belongs to the default package java.lang.
(a) Both Assertion (A) and Reason (R) are true and Reason (R) is a correct explanation of Assertion (A)
(b) Both Assertion (A) and Reason (R) are true and Reason (R) is not a correct explanation of Assertion (A)
(c) Assertion (A) is true and Reason (R) is false
(d) Assertion (A) is false and Reason (R) is true
(xvii) A student executes the following code to increase the value of a variable ‘x’ by 2.
He has written the following statement, which is incorrect.
x =+ 2;
What will be the correct statement?
A. x += 2;
B. x = 2;
C. x = x + 2;
(a) Only A
(b) Only C
(c) All the three
(d) Both A and C
(xviii) The statement used to find the total number of strings present in the String array String s[] is:
(a) s.length
(b) s.length()
(c) length(s)
(d) len(s)
(xix) Consider the following program segment in which the statements are jumbled. Choose the correct order of statements to swap two variables using the third variable.
void swap(int a, int b){
a = b; --> (1)
b = t; --> (2)
int t = 0; --> (3)
t = a; --> (4)
}
(a) (1) (2) (3) (4)
(b) (3) (4) (1) (2)
(c) (1) (3) (4) (2)
(d) (2) (1) (4) (3)
(xx) Assertion (A): An argument is a value that is passed to a method when it is called.
Reason (R): Variables which are declared in a method prototype to receive values are called actual parameters.
(a) Both Assertion (A) and Reason (R) are true and Reason (R) is a correct explanation of Assertion (A)
(b) Both Assertion (A) and Reason (R) are true and Reason (R) is not a correct explanation of Assertion (A)
(c) Assertion (A) is true and Reason (R) is false
(d) Assertion (A) is false and Reason (R) is true
Question 2
(i) Rewrite the following code using single if statement.
if(code == 'g')
System.out.println("GREEN");
else if(code == 'G')
System.out.println("GREEN")
OUTPUT:
if(code == 'g' || code == 'G')
System.out.println("GREEN");
(ii) Evaluate the given expression when the value of a = 2 and b = 3.
b *= a++ – ++b + ++a;
System.out.println(“a = ” + a);
System.out.println(“b = ” + b);
OUTPUT:
b = b * (a++ – ++b + ++a)
b = 3 * (2 – 4 + 4)
b = 3 * 2
b = 6
(iii) A student executes the following program segment and gets an error. Identify the statement which has an error. Correct the same to get the output as WIN.
boolean x = true;
switch(x){
case 1: System.out.println("WIN"); break;
case 2: System.out.println("LOOSE");
}
OUTPUT:
boolean x = true;
switch(x){
case true: System.out.println("WIN"); break;
case false: System.out.println("LOOSE");
}
(iv) Write the Java expression for:

OUTPUT:
Math.cbrt(x) + Math.sqrt(y)
(v) How many times will the following loop execute? Write the output of the code:
int x = 10;
while(true){
System.out.println(x++ * 2);
if(x % 3 == 0)
break;
}
OUTPUT:
20
22
The loop executes 2 times.
(vi) Write the output of the following String methods:
String x = “Galaxy”, y = “Games”;
(a) System.out.println(x.charAt(0) == y.charAt(0));
(b) System.out.println(x.compareTo(y));
OUTPUT:
(a) true
(b) -1
(vii) Predict the output of the following code snippet:
char ch = 'B';
char chr = Character.toLowerCase(ch);
int n = (int)chr - 10;
System.out.println((char)n + "\t" + chr);
OUTPUT:
OUTPUT:X b
(viii) A student is trying to convert the string present in x to a numerical value, so that he can find the square root of the converted value. However, the code has an error. Name the error (syntax/logical/runtime). Correct the code so that it compiles and runs correctly.
String x = "25";
int y = Double.parseDouble(x);
double r = Math.sqrt(y);
System.out.println(r);
OUTPUT:
It is a syntax error.
String x = "25";
int y = Integer.parseInt(x);
double r = Math.sqrt(y);
System.out.println(r);
(ix) Consider the following program segment and answer the questions below:
class calculate{
int a; double b;
calculate(){
a = 0;
b = 0.0;
}
calculate(int x, double y){
a = x;
b = y;
}
void sum(){
System.out.println(a * b);
}
}
Name the type of constructors used in the above program segment.
OUTPUT:
Non-parameterized and parameterized constructors.
(x) Consider the following program segment and answer the questions given below:
int x[][] = {{2, 4, 5, 6}, {5, 7, 8, 1}, {34, 1, 10, 9}};
(a) What is the position of 34?
(b) What is the result of x[2][3] + x[1][2]?
OUTPUT:
(a) x[2][0]
(b) 9 + 8 = 17
