- 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 the correct answers only)
(i)
Consider the above picture and choose the correct statement from the following:
(a) Polygon is the object and the pictures are classes.
(b) Both polygon and pictures are classes.
(c) Polygon is the class and the pictures are objects.
(d) Both polygon and the pictures are objects.
(ii) int x = 98; char ch = (char)x; What is the value in ch?
(a) b
(b) A
(c) B
(d) 97
(iii) The output of the statement “CONCENTRATION”.indexOf(‘T’) is:
(a) 9
(b) 7
(c) 6
(d) (-1)
(iv) The access specifier that gives least accessibility is:
(a) package
(b) public
(c) protected
(d) private
(v) The output of the statement “talent”.compareTo(“genius”) is:
(a) 11
(b) -11
(c) 0
(d) 13
(vi) Which of the following is an escape sequence character in Java?
(a) /n
(b) \t
(c) /t
(d) //n
(vii) if(a > b && b > c) then largest number is:
(a) b
(b) c
(c) a
(d) wrong expression
(viii) What is the output of Math.ceil(5.4) + Math.ceil(4.5)?
(a) 10.0
(b) 11.0
(c) 12.0
(d) 9.0
(ix) What is the method to check whether a character is a letter or digit?
(a) isDigit(char)
(b) isLetterOrDigit()
(c) isLetterOrDigit(char)
(d) isLETTERorDIGIT(char)
(x) The extension of a Java source code file is:
(a) exe
(b) obj
(c) jvm
(d) java
(xi) The number of bytes occupied by a character array of four rows and three columns are:
(a) 12
(b) 24
(c) 96
(d) 48
(xii) Which of the following data type cannot be used with switch case construct?
(a) int
(b) char
(c) String
(d) double
(xiii) Which of the following are entry-controlled loops?
1. for
2. while
3. do..while
4. switch
(a) only 1
(b) 1 and 2
(c) 1 and 3
(d) 3 and 4
(xiv) Method which reverses a given number is:
(a) Impure method
(b) Pure method
(c) Constructor
(d) Destructor
(xv) If the name of the class is “Yellow”, what can be the possible name for its constructors?
(a) yellow
(b) YELLOW
(c) Yell
(d) Yellow
(xvi) Invoking a method by passing the objects of a class is termed as:
(a) Call by reference
(b) Call by value
(c) Call by method
(d) Call by constructor
(xvii) The correct statement to create an object named mango of class fruit:
(a) Fruit Mango = new fruit();
(b) fruit mango = new fruit();
(c) Mango fruit = new Mango();
(d) fruit mango = new mango();
(xviii) Assertion (A): Static method can access static and instance variables.
Reason (R): Static variable can be accessed only by static method.
(a) Assertion and Reason both are correct.
(b) Assertion is true and Reason is false
(c) Assertion is false and Reason is true
(d) Assertion and Reason both are false
(xix) What is the output of the Java code given below?
String color[] = {“Blue”, “Red”, “Violet”};
System.out.println(color[2].length());
(a) 6
(b) 5
(c) 3
(d) 2
(xx) Which of the following mathematical methods returns only an integer?
(a) Math.ceil(n)
(b) Math.sqrt(n)
(c) Math.floor(n)
(d) Math.round(n)
Question 2
(i) Write Java expression for:
OUTPUT:
Math.abs(a + b) / Math.sqrt(a * a + b * b)
(ii) Evaluate the expression when x is 4:
x += x++ * ++x % 2;
x = x + (x++ * ++x % 2);
x = 4 + (4 * 6 % 2)
x = 4 + (24 % 2)
x = 4 + 0
x = 4
(iii) Rewrite the following do while program segment using for:
x = 10; y = 20;
do{
x++;
y++;
}while(x <= 20);
System.out.println(x * y);
x = 10; y = 20;
for(;;){
x++;
y++;
if(x > 20)
break;
}
System.out.println(x * y);
(iv) Give the output of the following program segment. How many times is the loop executed?
for(x = 10; x > 20; x++)
System.out.println(x);
System.out.println(x * 2);
OUTPUT: 20
The loop executes 0 times.
(v) String s1 = “45.50”; String s2 = “54.50”;
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
int x = (int)(d1 + d2);
What is the value of x?
OUTPUT:
x = 100
(vi) Consider the following two-dimensional array and answer the questions given below:
int x[][] = {{4, 3, 2}, {7, 8, 2}, {8, 3, 10}, {1, 2, 9}};
(a) What is the order of the array?
(b) What is the value of x[0][0] + x[2][2]?
OUTPUT:
Order is 4 × 3.
x[0][0] + x[2][2] = 4 + 10 = 14
(vii) Differentiate between boxing and unboxing.
OUTPUT:
Autoboxing/boxing is the automatic conversion of primitive data types to their corresponding wrapper classes when needed. Unboxing is the automatic conversion of wrapper class objects to their corresponding primitive data types when needed.
(viii) The following code to compare two strings is compiled, the following syntax error was displayed – incompatible types – int cannot be converted to boolean. Identify the statement which has the error and write the correct statement. Give the output of the program segment.
void calculate(){
String a = "KING", b = "KINGDOM";
boolean x = a.compareTo(b);
System.out.println(x);
}
OUTPUT:
The error is in line: boolean x = a.compareTo(b);
The correct statement is: int x = a.compareTo(b);
The output is: -3
(ix) Consider the given program and answer the questions given below:
class temp{
int a;
temp(){
a = 10;
}
temp(int z){
a = z;
}
void print(){
System.out.println(a);
}
void main(){
temp t = new temp();
temp x = new temp(30);
t.print();
x.print();
}
}
(a) What concept of OOPs is depicted in the above program with two constructors?
(b) What is the output of the method main()?
OUTPUT:
(a) Due to constructor overloading, the OOP Polymorphism is depicted.
(b) The output is:
10
30
(x) Primitive data types are built-in data types which are a part of the wrapper classes. These wrapper classes are encapsulated in the java.lang package. Non-primitive data types like Scanner class are a part of the utility package for which an object needs to be created.
(a) To which package the Character and Boolean classes belong?
(b) Write the statement to access the Scanner class in the program.
OUTPUT:
(a) The Character and Boolean classes belong to the java.lang package.
(b) The statement to access the Scanner class is: import java.util.Scanner;
class Overload{ public static void display(){ for(int i = 1; i <= 3; i++){ int value = 1; for(int j = 1; j <= 5; j++){ System.out.print(value + " "); if(value == 1) value = 2; else value = 1; } System.out.println(); } } public static void display(int n, int m){ if(m > n){ int q = m / n; System.out.println("Quotient = " + q); } else{ int sum = 2 * n + 3 * m; System.out.println("Sum = " + sum); } } public static double display(double a, double b, double c){ double p = (a + b) / c; double q = a + b + c; double z = p * q; System.out.println("z = " + z); return z; } }
