Chapter 4 Unit 3 Compiling and Executing Programs Web
Chapter 4 Unit 3 Compiling and Executing Programs Web
Question 1
Question 2
Question 3
Question 4
In a decision making program, if checks the true part and else the false.
True
Question 5
You may or may not include java.io.* package in a Stream Reader Class program.
False
Question 1
int x = 1,y = 1;
if(n>0)
{
x=x+1;
y=y+1;
}
Output
When n is 1:
x is 2 and y is 2
When n is 0:
x is 1 and y is 1
Explanation
When n is 1, if (n>0) is true. So the statements x=x+1; and y=y+1; are executed making the values of x
and y as 2.
When n is 0, if (n>0) is false. Statements x=x+1; and y=y+1; are not executed so values of x and y
remain 1.
Question 2
int b=3,k,r;
float a=15.15,c=0;
if(k==1)
{
r=(int)a/b;
System.out.println(r);
}
else
{
c=a/b;
System.out.println(c);
Output
Syntax Error
Explanation
1. Variable k is not initialized with any value before using it in the if check.
2. The closing curly brace is missing for the compound statement block of else part.
Question 3
int a=1,b=1,m=10,n=5;
if((a==1)&&(b==0))
{
System.out.println((m+n));
System.out.println((m-n));
}
if((a==1)&&(b==1))
{
System.out.println((m*n));
System.out.println((m%n));
}
Output
50
0
Explanation
Question 1
class public
{
public static void main(String args[])
{
int a=45,b=70,c=65.45;
sum=a+b;
diff=c-b;
System.out.println(sum,diff);
}
}
Answer
Corrected Program
Question 2
class Square
{
public static void main(String args[])
{
int n=289,r;
r=sqrt(n);
if(n==r)
System.out.println("Perfect Square");
else
System.out.println("Not a Perfect Square");
}
}
Answer
Corrected Program
class Square
{
public static void main(String args[])
{
int n=289,r;
r=(int)Math.sqrt(n); //1st & 2nd Corrections
if(n==r)
System.out.println("Perfect Square");
else
System.out.println("Not a Perfect Square");
}
}
Question 3
class Simplify
{
public static void main(String args[])
{
int a,b,c,d;
a=10,b=5,c=1,d=2;
c=a2+b2;
d=(a+b)2;
p=c/d;
System.out.println(c + " "+ " "+d+ " " +p);
}
}
Answer
class Simplify
{
public static void main(String args[])
{
int a,b,c,d;
a=10;b=5;c=1;d=2; //1st correction
c=a+b; //2nd correction
d=(a+b) / 2; //3rd correction
int p=c/d; //4th correction
System.out.println(c + " "+ " "+d+ " " +p);
}
}
Question 4
class Sample
{
public static void main(String args[])
{
int n,p;
float k,r;
n=25;p=12;
if(n=25)
{
k=pow(p,2)
System.out.println("The value of"+p+ "= "+k);
}
else
{
r=Math.square root(n);
System.out.println("The value of"+n+ "= "+r);
}
}
}
Answer
1. if(n=25) should be if(n==25)
2. Correct way to call method pow is Math.pow(p,2);
3. Return type of Math.pow is double. Variable k is of float type so return value should be explicitly
casted to float.
4. Math.square root(n) should be Math.sqrt(n)
5. Return type of Math.sqrt is double. Variable r is of float type so return value should be explicitly
casted to float.
Corrected Program
class Sample
{
public static void main(String args[])
{
int n,p;
float k,r;
n=25;p=12;
if(n==25) //1st correction
{
k=(float)Math.pow(p,2); //2nd & 3rd corrections
System.out.println("The value of"+p+ "= "+k);
}
else
{
r=(float)Math.sqrt(n); //4th & 5th corrections
System.out.println("The value of"+n+ "= "+r);
}
}
}
Question 1
What are the three ways to input the values in a Java Programming?
Answer
Question 2
Answer
Answer
Answer
if (condition 1) {
Statement a;
Statement b;
..
}
else {
Statement c;
Statement d;
..
}
Question 3
Answer
Syntax Errors occur when we violate the rules of writing Logical Errors occur due to our mistakes in
the statements of the programming language. programming logic.
Question 4
What are the different types of errors that take place during the execution of a program?
Answer
Logical errors and Run-Time errors occur during the execution of the program.
Question 5
Answer
Two or more statements can be grouped together by enclosing them between opening and closing curly
braces. Such a group of statements is called a compound statement.
if (a < b) {
/*
* All statements within this set of braces
* form the compound statement
*/
Question 6
Answer
if-else-if ladder construct is used to test multiple conditions and then take a decision. It provides multiple
branching of control. Below is an example of if-else-if:
Question 7
Answer
import keyword is used to import built-in and user-defined packages into our Java program.
Question 8
Answer
Question 9
Answer
Question 10
What are the points to be taken care while naming a class in a Java program?
Answer
A class name should be a valid Java identifier i.e. it should follow the below three rules:
1. Name of the class should be a sequence of alphabets, digits, underscore and dollar sign characters
only.
2. It should not start with a digit.
3. It should not be a keyword or a boolean or null literal.
import java.io.*;
Output
Question 2
Output
Question 3
Write a program to input the temperature in Celsius and convert it into Fahrenheit. If the temperature is more
than 98.6 °F then display "Fever" otherwise "Normal".
import java.io.*;
Write a program to accept marks of a student obtained in 5 different subjects (English, Phy., Chem., Biology,
Maths.) and find the average. If the average is 80% or more then he/she is eligible to get "Computer Science"
otherwise "Biology".
import java.io.*;
Question 5
Write a program to accept three angles of a triangle and check whether the triangle is possible or not and display
the message accordingly.
import java.io.*;
int sum = a + b + c;
Write a program to accept a number and check whether the number is a perfect square or not.
Sample Input: 49
Sample Output: A perfect square
import java.io.*;
if (diff == 0) {
System.out.println(a + " is a perfect square");
}
else {
System.out.println(a + " is not a perfect square");
}
}
}
Output
Question 7
A shopkeeper announces two successive discounts 20% and 10% on purchasing of goods on the marked price.
Write a program to input marked price and calculate the selling price of an article.
import java.io.*;
double d1 = mp * 20 / 100.0;
double amt1 = mp - d1;
Question 8
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
import java.io.*;
a = a + b;
b = a - b;
a = a - b;
Question 9
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
import java.io.*;
double i1 = p * r * 1 / 100.0;
double amt = p + i1;
System.out.println("Interest for the first year: " + i1);
Output
Question 10
'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 name of the customer, discount and the amount to be paid after
discount.
import java.io.*;
public class KboatDiscount
{
public static void main(String args[]) throws IOException {
int d = 0;
if (c <= 2000)
d = 5;
else if (c <= 5000)
d = 10;
else if (c <= 10000)
d = 15;
else
d = 20;
Output