- 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
a) Why is a class called a factory of objects?
A class is called a factory of objects because with one class definition, we can create several objects, each with their own state and behavior.
b) State the difference between a boolean literal and a character literal.
A boolean literal occupies 1 byte of storage, whereas a character literal occupies 2 bytes of storage. A boolean literal can store either true or false, whereas a character literal can store Unicode characters.
c) What is the use and syntax of a ternary operator?
A ternary operator is an alternative for if-else statement. It works with three operands.
Following is the syntax:
variable = (condition)? value 1 : value 2;
d) Write one word answer for the following:
(i) A method that converts a string to a primitive integer data type.
Integer.parseInt()
(ii) The default initial value of a boolean variable data type.
false
e) State one similarity and one difference between while and for loop.
Similarity: Both while and for loops are entry-controlled loops.
Difference: The while loop is used when the number of iterations is not known, whereas a for loop is used when the number of iterations is known.
Question 2
a) Write the function prototype for the function “sum” that takes an integer variable x as its argument and returns a value of float data type.
float sum(int x)
b) What is the use of the keyword this?
The keyword this is used to refer to the currently calling object.
c) Why is a class known as composite data type?
A class is known as a composite data type because it is generally user-defined, and it is built using one or more primitive data types.
d) Name the keyword that:
(i) is used for allocating memory to an array.
new
(ii) causes the control to transfer back to the method call.
return
e) Differentiate between pure and impure functions.
A pure function does not change the state of an object, whereas an impure function changes the state of an object.
Question 3
a) Write an expression for:

Math.pow(a + b, n) / (Math.sqrt(3) + b)b) The following is a segment of a program:
x = 1; y = 1;
if(n > 0)
{
x = x + 1;
y = y - 1;
}
What will be the value of x and y, if n assumes a value (i) 1 (ii) 0?
(i) x = 2, y = 0
(ii) x = 1, y = 1
c) Analyze the following program segment and determine how many times the body of the loop will be executed (show the working):
x = 5; y = 50;
while(x <= y)
{
y = y / x;
System.out.println(y);
}
y = 50 –> y = 10 –> y = 2
The loop body executes 2 times.
d) When there are multiple definitions with the same function name, what makes them different from each other?
The number of arguments.
The data types of each argument.
e) Given that int x[][] = {{2, 4, 6}, {3, 5, 7}};
What will be the value of x[1][0] and x[0][2]?
x[1][0] = 3
x[0][2] = 6
switch(opn)
{
case \'a\':
System.out.println("Platform Independent");
break;
case \'b\':
System.out.println("Object Oriented");
case \'c\':
System.out.println("Robust and Secure");
break;
default:
System.out.println("Wrong Input");
}
(i) When opn = ‘b’, the output is:
Object Oriented
Robust and Secure
(ii) When opn = ‘x’, the output is:
Wrong Input
(iii) When opn = ‘a’, the output is:
Platform Independent
class academic
{
int x, y;
void access()
{
int a, b;
academic student = new academic();
System.out.println("Object created");
}
}
(i) What is the object name of class academic?
The object name is student.
(ii) Name the class variables used in the program.
The class variables are x and y.
(iii) Write the local variables used in the program.
The local variables are a and b.
(iv) Give the type of function used and its name.
The type of function is void. It is used to indicate that the function doesn’t return any value.
h) Convert the following segment into an equivalent do loop:
int x, c;
for(x = 10, c = 20; c > 10; c = c - 2)
x++;
int x = 10, c = 20;
while(c > 10){
c = c - 2;
x++;
}
