JAVA CODES
JAVA CODES
Answer
if(condition )
Statement
OR
if(condition)
{
Statement 1
Statement 2
:
:
Statement n
}
Question 2
Answer
A set of two or more statements which are executed together and are placed within curly
brackets { } is called compound statement.
For example,
int n=5;
if(n>0)
{
System.out.println(n);
n=n-1;
}
In this snippet, the statements System.out.println(n); and n=n-1; are compound
statements as they will be executed only when the condition of if is true.
Question 3
It is a sequential movement of the control from one statement to another from beginning to
the end of the program. It is the simplest way of executing a program, where there is no
restriction on flow of control during the process.
Sometimes, it may happen that a statement or a set of statements is executed only when a
given condition results in true. However, if the condition results in false, then these
statements are ignored and the control moves forward to the next statement of the program.
This type of flow of control is known as the conditional flow of control and the statements
are known as the decision making statements.
1. if statement
2. if-else statement
3. if-else-if statement
Question 4
Write down the syntax of the following with reference to Java Programming:
Answer
Question 5
What are the different types of errors that may occur during the execution of a program?
Answer
Logical errors and Run-Time errors occur during the execution of the program.
Question 6
Answer
Syntax Errors Logical Errors
Syntax Errors occur when we violate the rules of writing the Logical Errors occur due to our mista
statements of the programming language. programming logic.
Question 7
Answer
In if-else construct, the user defines the statements to be executed when the condition results
in true or false. If the given condition results in true, the statements in the 'if' block are
executed and if the given condition results in false, the statements in the 'else' block are
executed.
if(condition)
Statement 1 //executed if condition is true
else
Statement 2 //executed if condition is false
(b) if-else-if construct
It is used when more than one condition are to be evaluated and only one of them is true.
if(condition 1)
Statement 1 //executed if condition 1 is true
else if(condition 2)
Statement 2 //executed if condition 2 is true
else
Statement 3 //executed if both conditions 1 and 2 are
false
When 'condition 1' is 'True' then it will execute statement 1. In case 'condition 1' is false, the
control will move to 'else if' part and will check 'condition 2'. If it is true, the control will
execute 'Statement 2' otherwise it will execute 'Statement 3'.
Question 1
Write a program to calculate and display the value of the given expression:
(a2+b2)/(a-b),when a=20, b=15
Output
Question 2
Output
Question 3
Write a program to accept Principal, Rate and Time. Calculate and display the interest
accumulated for the first year, second year and the third year compound annually.
Sample Input:
Principal = ₹5,000, Rate = 10% per annum, Time = 3 years
Sample Output:
Interest for the first year: ₹500
Interest for the second year: ₹550
Interest for the third year: ₹605
public class KboatInterest
{
public static void main(double p, double r, int t) {
double i1 = p * r * 1 / 100.0;
double amt = p + i1;
System.out.println("Interest for the first year: " +
i1);
Output
Question 4
A shopkeeper announces two successive discounts 20% and 10% on purchasing of goods on
the marked price. Write a program to input marked price. Calculate and display the selling
price of the article.
Output
Question 5
Write a program to input two unequal numbers. Display the numbers after swapping their
values in the variables without using a third variable.
Sample Input: a=23, b= 56
Sample Output: a=56, b=23
Output
Question 6
Write a program to input the temperature in Celsius and then convert it into Fahrenheit. If the
temperature in Fahrenheit is more than 98.6°F then display "Fever", otherwise "Normal".
Output
Question 7
Write a program to accept the marks, obtained in five different subjects (English, Physics,
Chemistry, Biology, Maths) by a student and find the average marks. If the average is 80 or
more, then display he/she is eligible to get "Computer Science', otherwise "Biology".
Output
Question 8
'Mega Market' has announced festival discounts on the purchase of items, based on the total
cost of the items purchased:
Up to ₹2,000 5%
Write a program to input the total cost. Display the discount and amount to be paid after
discount.
if (c <= 2000)
d = 5;
else if (c <= 5000)
d = 10;
else if (c <= 10000)
d = 15;
else
d = 20;