Escolar Documentos
Profissional Documentos
Cultura Documentos
Practice Questions
----------------------------->Objective-Type Questions<------------------------------
State whether True or False:
1. A statement missing a semicolon is an example of logical error. [False]
2. You can input only integers using parameterized inputs. [False]
3. Using nextInt( ) member function of the Scanner class, you can input a string. [False]
4. While you input numbers of float data type using Scanner, succeed the number with f?[False]
5. Parameterised input for String requires you to enter data within quotes. [True]
----------------------------->Subjective-Type Questions<-----------------------------
Answer the following questions:
1. Rewrite the following program after removing the errors in the program, if any:
class Myclass
{
import java.util.Scanner;
static void function( )
{
int a;
a=sc.nextInt( );
Scanner sc=new Scanner(System.in);
System.out.println(a);
}
Ans. All corrections made are underlined:
import java.util.Scanner;
class Myclass
{
static void function( )
{
int a;
System.out.println(“Enter an integer:”);
a=sc.nextInt( );
System.out.println(a);
}
}
Lab Exercises
Write the following programs:
1. Write a program to input two integers using parameters and find their sum and
product.
Ans. public class Question1
{
static void main(int a,int b)
{
int s,p;
s=a+b;
p=a*b;
System.out.println("Sum="+s);
System.out.println("Product="+p);
}
}
2. Write a program to input 5 integers and find their average.
Ans. public class Question2
{
static void main(int a,int b,int c,int d,int e)
{
float avg;
avg=(a+b+c+d+e)/5;
System.out.println("Average="+avg);
}
}
3. Write a program to input two integers using Scanner and find the product of
their sum and difference.
Ans. import java.util.*;
public class Question3
{
static void main()
{
Scanner sc=new Scanner(System.in);
int a,b,s,d,p;
System.out.println("Enter two integers:");
a=sc.nextInt();
b=sc.nextInt();
s=a+b;
d=a-b;
p=s*d;
System.out.println("Sum="+s);
System.out.println("Difference="+d);
System.out.println("Product of Sum and Difference="+p);
}
}
4. Write a program to input three integers and find the difference between their
sum and their average.
Ans. import java.util.*;
public class Question4
{
static void main()
{
Scanner sc=new Scanner(System.in);
int a,b,c,sum;
float avg,diff;
System.out.println("Enter three integers:");
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
sum=a+b+c;
avg=(float)sum/3;
diff=sum-avg;
System.out.println("Difference="+diff);
}
}
5. Write a program to input the Principal, Rate and Time for a certain amount of
money and print the Simple Interest.
(𝑷𝒓𝒊𝒏𝒄𝒊𝒑𝒍𝒆 ∗𝑹𝒂𝒕𝒆∗𝑻𝒊𝒎𝒆)
Note: Simple Interest =
𝟏𝟎𝟎
Ans. import java.util.*;
public class Question5
{
static void main()
{
Scanner sc=new Scanner(System.in);
float principal,rate,time,SI;
System.out.println("Enter Principal, Rate and Time:");
principal=sc.nextFloat();
rate=sc.nextFloat();
time=sc.nextFloat();
SI=(principal*rate*time)/3;
System.out.println("Simple Interest="+SI);
}
}
6. Write a program to input the length and breadth of a rectangle and find its area and
perimeter.
Note: Area of a rectangle=length*breadth
Perimeter of a rectangle=2*(length + breadth)
Ans. import java.util.*;
public class Question6
{
static void main()
{
Scanner sc=new Scanner(System.in);
float length,breadth,area,perimeter;
System.out.println("Enter length and breadth of a rectangle:");
length=sc.nextFloat();
breadth=sc.nextFloat();
area=length*breadth;
perimeter=2*(length+breadth);
System.out.println("Area="+area);
System.out.println("Perimeter="+perimeter);
}
}
7. Write a program to input the radius of a circle and find its area and circumference.
Note: Area of a circle=22/7 * radius2
Circumference of a circle= 2 * 22/7 * radius
Ans. import java.util.*;
public class Question7
{
static void main()
{
Scanner sc=new Scanner(System.in);
float radius,area,circumference;
System.out.println("Enter radius of a circle:");
radius=sc.nextFloat();
area=(float)22/7*radius*radius;
circumference=2*(float)22/7*radius;
System.out.println("Area="+area);
System.out.println("Circumference="+circumference);
}
}
8. Write a program to input the length, breadth and height of a cuboid and find its Volume
and Total Surface Area.
Note: Volume of a cuboid= length*breadth*height
Total Surface Area=2*(length*breadth+breadth*height+height*length)
Ans. import java.util.*;
public class Question8
{
static void main()
{
Scanner sc=new Scanner(System.in);
float length,breadth,height,vol,area;
System.out.println("Enter length, breadth and height of a cuboid:");
length=sc.nextFloat();
breadth=sc.nextFloat();
height=sc.nextFloat();
vol=length*breadth*height;
area=2*(length*breadth+breadth*height+height*length);
System.out.println("Volume of cuboid="+vol);
System.out.println("Total Surface Area="+area);
}
}
9. Write a program to input the radius and height of a cylinder and find its volume and
total surface area.
Note: Total Surface Area=2πr2 + 2πrh
Volume= πr2h
Ans. import java.util.*;
public class Question9
{
static void main()
{
Scanner sc=new Scanner(System.in);
float radius,height,vol,area;
System.out.println("Enter radius and height of a cylinder:");
radius=sc.nextFloat();
height=sc.nextFloat();
vol=(float)22/7*radius*radius*height;
area=2*(float)22/7*radius*(radius+height);
System.out.println("Volume of cylinder="+vol);
System.out.println("Total Surface Area="+area);
}
}
10. Write a program to input three integers and find the sum of the last digit of the
numbers.
For example, if the inputs are: 12
26
35
Output:
Sum of the last digit of the integers are: 13
Ans. import java.util.*;
public class Question10
{
static void main()
{
Scanner sc=new Scanner(System.in);
int a,b,c,s;
System.out.println("Enter three integers:");
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
s=a%10+b%10+c%10;
System.out.println("Sum of the last digit of the integers are:"+s);
}
}
11. Write a program to input the temperature in Fahrenheit and change it to Celsius.
Note: The relation between Fahrenheit and Celsius is given by the formula:
𝑪 𝐅 − 𝟑𝟐
=
𝟓 𝟗
Where C=Celsius and F=Fahrenheit
Ans. import java.util.*;
public class Question11
{
static void main()
{
Scanner sc=new Scanner(System.in);
float f,c;
System.out.println("Enter temperature in fahrenheit:");
f=sc.nextInt();
c=(f-32)*5/9;
System.out.println("Temperature in Celcius:"+c);
}
}
12. Write a program to input time in seconds and display the time broken up as hours,
minutes and seconds.
For Example:
INPUT: Enter Time in Seconds: 4326
OUTPUT:
Time in hours:1
Time in minutes:12
Time in seconds:6
Ans. import java.util.*;
public class Question12
{
static void main()
{
Scanner sc=new Scanner(System.in);
int s,h,m,ss;
System.out.println("Enter temperature in seconds:");
s=sc.nextInt();
h=s/3600;
m=(s%3600)/60;
ss=(s%3600)%60;
System.out.println("Time in hours:"+h);
System.out.println("Time in minutes:"+m);
System.out.println("Time in seconds:"+ss);
}
}
13. Write a program to input a floating point number and round it off to the nearest
integer.
For Example:
INPUT: Enter a floating point number: 12.3
OUTPUT: Rounded off to nearest integer:12
INPUT: Enter a floating point number: 14.5
OUTPUT: Rounded off to nearest integer:15
Ans. import java.util.*;
public class Question13
{
static void main()
{
Scanner sc=new Scanner(System.in);
float f;
int n;
System.out.println("Enter a floating point number:");
f=sc.nextFloat();
n=(int)(f+0.5);
System.out.println("Rounded off to nearest integer:"+n);
}
}
14. Write a program to input a floating point number and round it off to two places of
decimal.
For Example:
INPUT: Enter a floating point number: 12.367
OUTPUT: Rounded off to two places of decimal:12.37
INPUT: Enter a floating point number: 14.563
OUTPUT: Rounded off to two places of decimal:14.56
Ans. import java.util.*;
public class Question14
{
static void main()
{
Scanner sc=new Scanner(System.in);
float f,n;
int h;
System.out.println("Enter a floating point number:");
f=sc.nextFloat();
h=(int)((f*100)+0.5);
n=(float)h/100;
System.out.println("Rounded off to two places of decimal:"+n);
}
}
15. Write a program to input two integers (say a and b) and interchange their values and
display the result.
For Example:
INPUT: Enter two integers: 15 36
OUTPUT:
Before interchange: a=15 and b=36
After interchange: a=36 and b=15
Please note that you can take just one variable other than a and b for interchanging.
Ans. import java.util.*;
public class Question15
{
static void main()
{
Scanner sc=new Scanner(System.in);
int a,b,c;
System.out.println("Enter two integers:");
a=sc.nextInt();
b=sc.nextInt();
System.out.println("Before interchange: a="+a+" and b="+b);
c=a;
a=b;
b=c;
System.out.println("After interchange: a="+a+" and b="+b);
}
}
16. Write a program to input the time in hours, minutes and seconds and print it in
seconds.
For Example:
INPUT: Enter time in hours:
1
Enter time in minutes:
12
Enter time in seconds:
6
OUTPUT:
Time in seconds:43261
Ans. import java.util.*;
public class Question16
{
static void main()
{
Scanner sc=new Scanner(System.in);
int h,m,s,ss;
System.out.println("Enter time in hours:");
h=sc.nextInt();
System.out.println("Enter time in minutes:");
m=sc.nextInt();
System.out.println("Enter time in seconds:");
s=sc.nextInt();
ss=h*3600+m*60+s;
System.out.println("Time in seconds:"+ss);
}
}
21
17. Write a program to input three integers and find their sum, without using the
mathematical operator +.
Ans. import java.util.*;
public class Question17
{
static void main()
{
Scanner sc=new Scanner(System.in);
int a,b,c,s;
System.out.println("Enter three integers:");
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
s=a-(-b)-(-c);
System.out.println("Sum:"+s);
}
}
18. Write a program to enter the perimeter of a square and find its perimeter.
For Example,
INPUT:
Enter the perimeter of a square: 64
OUTPUT:
Area of the square is: 256
Ans. import java.util.*;
public class Question18
{
static void main()
{
Scanner sc=new Scanner(System.in);
float p,s,a;
System.out.println("Enter the perimeter of a square:");
p=sc.nextInt();
s=p/4;
a=s*s;
System.out.println("Area of the square:"+a);
}
}
19. Write a program to enter the length and area of a rectangle and find its perimeter.
For Example,
INPUT:
Enter the length of the rectangle:12
Enter the area of the rectangle: 72
OUTPUT:
Perimeter of the rectangle:36.0
Ans. import java.util.*;
public class Question19
{
static void main()
{
Scanner sc=new Scanner(System.in);
float l,b,a,p;
System.out.println("Enter the length of the rectangle:");
l=sc.nextInt();
System.out.println("Enter the area of the rectangle:");
a=sc.nextInt();
b=a/l;
p=2*(l+b);
System.out.println("Perimeter of the rectangle:"+p);
}
}
20. Write a program to input the basic pay of an employee and find the gross pay of the
employee for the following allowances and deductions:
Dearness Allowance = 25% of Basic Pay
House Rent Allowance=15% of Basic Pay
Provident Fund=8.33% of Basic Pay
Net Pay=Basic Pay + Dearness Allowance + House Rent Allowance
Gross Pay= Net Pay – Provident Fund
Ans. import java.util.*;
public class Question20
{
static void main()
{
Scanner sc=new Scanner(System.in);
float basic,da,hra,pf,np,gp;
System.out.println("Enter the basic pay of the employee:");
basic=sc.nextFloat();
da=(float)25/100*basic;
hra=(float)15/100*basic;
pf=(float)8.33/100*basic;
np=basic+da+hra;
gp=np-pf;
System.out.println("Gross Pay:"+gp);
}
}
21. Write a program to input the dimensions of a rectangle and find the area of
that square whose perimeter is equal to the perimeter of the rectangle.
Ans. import java.util.*;
public class Question21
{
static void main()
{
Scanner sc=new Scanner(System.in);
float l,b,p,s,a;
System.out.println("Enter the length & breadth of the rectangle:");
l=sc.nextFloat();
b=sc.nextFloat();
p=2*(l+b); //perimeter of the rectangle
s=p/4; /*side of the square whose perimeter is equal
to the perimeter of the rectangle*/
a=s*s; //area of the square
System.out.println("Area of the Square:"+a);
}
}
22. Write a program to input an integer and if positive change it to negative and if
negative change it to positive.
INPUT: Enter an integer: 12
OUTPUT: Sign changed: –12
INPUT: Enter an integer: –14
OUTPUT: Sign changed: 14
Ans. import java.util.*;
public class Question22
{
static void main()
{
Scanner sc=new Scanner(System.in);
int a,b;
System.out.print("Enter an integer: ");
a=sc.nextInt();
b=-a;
System.out.println("Sign Changed: "+b);
}
}
23. Write a program to input two integers into two variables (say a and b) and
interchange them by:
i) using a third variable (say c),
ii) without using a third variable.
Ans. (i) import java.util.*;
public class Question23i
{
static void main()
{
Scanner sc=new Scanner(System.in);
int a,b,c;
System.out.println("Enter two integers: ");
a=sc.nextInt();
b=sc.nextInt();
System.out.println("Before interechange: a="+a+",b="+b);
c=a;
a=b;
b=c;
System.out.println("After interechange: a="+a+",b="+b);
}
}
(ii)import java.util.*;
public class Question23ii
{
static void main()
{
Scanner sc=new Scanner(System.in);
int a,b;
System.out.println("Enter two integers: ");
a=sc.nextInt();
b=sc.nextInt();
System.out.println("Before interechange: a="+a+",b="+b);
a=a+b;
b=a-b;
a=a-b;
System.out.println("After interechange: a="+a+",b="+b);
}
}
24. If the marks obtained by a student in five different subjects are input through
the keyboard, write a program to find out the aggregate marks and percentage
marks obtained by the student. Assume that the maximum marks that can be
obtained by a student in each subject is 100.
Ans. import java.util.*;
public class Question24
{
static void main()
{
Scanner sc=new Scanner(System.in);
int a,b,c,d,e,agg;
float perc;
System.out.println("Enter marks in 5 subjects: ");
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
d=sc.nextInt();
e=sc.nextInt();
agg=a+b+c+d+e;
perc=(float)agg/500*100; /*Assuming maximum marks
in each subject is 100*/
System.out.println("Aggregate Marks="+agg);
System.out.println("Percentage Marks="+perc);
}
}
25. If the total selling price of 15 items and the total profit earned on them is input
through the keyboard, write a program to find the cost price of one item.
Ans. import java.util.*;
public class Question25
{
static void main()
{
Scanner sc=new Scanner(System.in);
float sp,profit,cp,cpOfOne;
System.out.println("Enter total Selling Price of 15 items: ");
sp=sc.nextFloat();
System.out.println("Enter total profit of 15 items: ");
profit=sc.nextFloat();
cp=sp-profit; //Total Cost Price
cpOfOne=cp/15; //Cost Price of one item.
System.out.println("Cost Price Of one Item="+cpOfOne);
}
}
26. Write a program to input three integers and find their product without the '*'
operator.
Ans. import java.util.*;
public class Question26
{
static void main()
{
Scanner sc=new Scanner(System.in);
int a,b,c,p;
float f,e;
System.out.println("Enter 3 integers: ");
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
f=(float)1/b;
e=(float)1/c;
p=(int)(a/f);
p=(int)(p/e);
System.out.println("Product="+p);
}
}
27. Write a program to input an integer and raise it to the power of 2 and display it.
INPUT: Enter an integer: 4
OUTPUT: Power of 2 is: 16
INPUT: Enter an integer: 6
OUTPUT: Power of 2 is: 64
(Hint: use the shift operator)
Ans. import java.util.*;
public class Question27
{
static void main()
{
Scanner sc=new Scanner(System.in);
int a,b;
System.out.print("Enter an integer: ");
a=sc.nextInt();
b=2<<(a-1);
System.out.println("Power of 2 is:"+b);
}
}
122