- 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 answers to the questions from the given options.
(Do not copy the questions, write only the correct answers.)
(i) Character class methods are found in the package called:
(a) java.util
(b) java.lang
(c) java.awt
(d) java.io
(ii) System.out.println(‘Z’ + 32); will display:
(a) z
(b) Z
(c) 122
(d) 154
(iii) double x[] = {2.5, 4.5, 5.5, 6.4}; occupies ____ bytes.
(a) 16
(b) 4
(c) 8
(d) 32
(iv) The output of 42 / 6 % 2 is:
(a) 1
(b) 10
(c) 2
(d) 0
(v)
Consider the two-dimensional array P[2][3], of peripherals (input/output devices) given above, state the index of the device Barcode Scanner.
(a) P[1][1]
(b) P[0][1]
(c) P[1][2]
(d) P[0][0]
(vi) Which of the following is user-defined data type?
(a) only 1
(b) 1 and 3
(c) only 2
(d) only 4
(vii) Select the infinite loop:
(a) for(int i = 1; i <= 10; i++)
(b) for(int i = 2; i != 0; i -= 3)
(c) for(int i = 5; i <= 5; i++)
(d) for(int i = 1; i >= 1; i–)
(viii) The output of Math.max(-7, Math.min(-5, -4)) is:
(a) -5
(b) -4
(c) -7
(d) error
(ix) Which of the following is true for the given object creation statement?
Game cricket = new Game();
(a) Game is an object of cricket class
(b) New keyword creates object Game
(c) Game is a class and cricket is an object
(d) Game and cricket are objects
(x) Post Office is an example for ____ access specifier.
(a) public
(b) local
(c) protected
(d) private
(xi) Assertion (A): In switch case, break statement avoids fall through.
Reason (R): break statement helps to execute only one case at a time.
(a) Both (A) and (R) are true and (R) is a correct explanation of (A).
(b) Both (A) and (R) are true and (R) is not a correct explanation of (A).
(c) (A) is true and (R) is false.
(d) (A) is false and (R) is true.
(xii) A physical education teacher asks the students to do the side stretch as shown below, 10 times. Which programming construct the teacher uses?
(a) if
(b) switch
(c) for
(d) if else if
(xiii) The index (subscript) of the last element of an array ar[] is:
(a) ar.length()
(b) ar[].length
(c) ar.length() – 1
(d) ar.length – 1
(xiv) Assertion (A): A clock is a real-life example of nested loops.
Reason (R): The hour hand moves through 12 positions, while the minute hand moves through 60 positions within each hour.
(a) Both (A) and (R) are true and (R) is a correct explanation of (A).
(b) Both (A) and (R) are true and (R) is not a correct explanation of (A).
(c) (A) is true and (R) is false.
(d) (A) is false and (R) is true.
(xv) Which of the following pairs of methods will cause a compile-time error due to incorrect method overloading?
(a) void test(int a, int b) and void test(double a, double b)
(b) void test(int a, double b) and void test(double a, int b)
(c) void test(int a, double b) and void test(int a)
(d) void test(int a) and int test(int a)
(xvi) Which of the following converts “25” to 25.0?
(a) Double.Parsedouble(“25”)
(b) Double.parse(“25”)
(c) Double.parseDouble(“25”)
(d) Double.parseDouble(25)
(xvii) Consider the program segment:
int p = 0;
for(p = 4; p > 0; p -= 2);
System.out.print(p);
System.out.println(p);
The above statement will display:
(a) 42
(b) 4200
(c)
0
0
(d) 00
(xviii) System.out.println(“I said, \”It\’s wise to obey elders.\””);
The output of the above statement is:
(a) I said, ‘It is wise to obey elders.’
(b) I said, “It’s wise to obey elders.”
(c) I said, It’s wise to elders.
(d) “‘It’s wise to obey elders.'”
(xix) What is the output of the statement given below?
“ANGER”.compareTo(“ANGEL”)
(a) 3
(b) -6
(c) 6
(d) 0
(xx) Consider the following program segment in which the statements are jumbled.
Choose the correct order of statements to calculate and return the factorial of 4.
for(k = 1; k <= 4; k++) → 1
return fa; → 2
long fa = 1, k; → 3
fa *= k; → 4
(a) 1, 2, 3, 4
(b) 3, 1, 4, 2
(c) 3, 1, 2, 4
(d) 1, 3, 2, 4
Question 2
(i) Write the Java expression to find the product of square root of P and the square root of Q using the methods of Math class.
OUTPUT:
Math.sqrt(P) * Math.sqrt(Q)
(ii) Write the output of the following String method:
String x = “talent”;
String y = “matrix”;
System.out.println(x.substring(3).concat(y.substring(3)));
OUTPUT:
entrix
(iii) Write the Java statement for creating an object named ‘sifra’ of the class ‘Robot’, which takes three double parameters.
OUTPUT:
Robot sifra = new Robot(2.5, 3.5, 4.5);
(iv) Convert the given loop into exit-controlled loop.
int a, b;
for(a = 10, b = 1; a >= 1; a -= 2){
b += a;
b++;
}
OUTPUT:
System.out.println(b);int a = 10, b = 1;
do{
b += a;
b++;
a -= 2;
}while(a >= 1);
System.out.println(b);
(v) Consider and give the output of the following program:
class report{
int a, b;
report(){
a = 10;
b = 15;
}
report(int x, int y){
a = x;
b = y;
}
void print(){
System.out.println(a * b);
}
static void main(){
report r = new report();
r.print();
report p = new report(4, 5);
p.print();
}
}
150
20
(vi) (a) Name one String method which results in positive integer only.
OUTPUT:
length()
(b) Name one String method which results in a character.
OUTPUT:
charAt()(vii) John was asked to write a Java code to calculate the surface area of a cone. The following code was written by him:
Surface area of cone is A = πrl
class area{
double area(double r, double h){
double l, a;
a = 22.0 / 7 * r * l;
l = Math.sqrt(r * r + h * h);
return a;
}
}
Specify the type of the error in the above program, correct and write the program to be error free.
OUTPUT:
Compile-time errorclass area{
double findArea(double r, double h){
double l, a;
l = Math.sqrt(r * r + h * h);
a = 22.0 / 7 * r * l;
return a;
}
}
(viii) Consider the following array and answer the questions given below:
int a[] = {12, 10, 8, 4, 6, 2, 3, 5, 7};
(a) What is the output of System.out.print(a[0] + a[5]);?
OUTPUT:
14(b) What is the index (subscript) of the largest element of the array a[]?
OUTPUT:
0
(ix) (a) Write the Java statement to initialize the first 6 odd numbers in a 3 × 2 array.
OUTPUT:
a[3][2] = {{1, 3}, {5, 7}, {9, 11}};
(b) What is the result of x[0][1] + x[2][1] of the above array?
OUTPUT:
14(x) Give the output of the following program segment and specify how many times the loop is executed.
String x = "JAVA";for(i = 0; i < s.length(); i += 2) System.out.println(s.substring(i));
OUTPUT:
JAVA
VA
