Multiple Choice Questions
1 1. Which one of the following options is an example of arithmetic operator ?
a. + b. >
c. ! d. =
2 2. Looping is used to :
a. Store values b. Validate code
c. Repeat statements d. Run code
3 3. Which one of the following options is the problem or issue that occurs during the execution of the program ?
a. Exception b. Function
c. Try d. Catch
4 4. Which one of the following option is used to end a loop ?
a. while b. break
c. try d. void
5 5. Which one of the following options is used to perform a set of operations or tasks ?
a. Void b. Exception Handling
c. Exceptions d. Function
True or False :-
11. A function cannot return a value. FALSE
22. Arrays store values of different data types. FALSE
33. A try block must always be followed by a catch block. TRUE
44. Exceptions are the errors that occur at runtime. TRUE
55. A function can be called from the main function. TRUE
Subjective Qustions :-
Qus. - 01 : What is the difference between conditional constructs and loops ?
Ans:
Qus. - 02 : What are functions ? Explain in detail.
Ans:- A function is a group of statements that performs a specific task. Functions are also known as
methods.
Syntax of Function :-
<modifier> <return_type> <function_name> ( )
{
/*function body*/
}
ex:-
public void addition ( )
{
int num1,num2,result;
result=num1+num2;
System.out.println("Result of Addition is = " + result);
}
Ans:- A function is a group of statements that performs a specific task. Functions are also known as
methods.
Syntax of Function :-
<modifier> <return_type> <function_name> ( )
{
/*function body*/
}
ex:-
public void addition ( )
{
int num1,num2,result;
result=num1+num2;
System.out.println("Result of Addition is = " + result);
}
Qus. - 03 : What are arrays used for ?
Ans. :- Arrays are objects that store multiple values of the same data type in a computer memory location. Each value in an array is called an element, which can be accessed by a numerical number called index.An array index usually starts from Zero(0).
The syntax to declare an array is :-
<data_type> <variable_name> [ ];
The syntax to instantiate an array is :-
<data_type> <variable_name> [ ] = new <data_type> [<array_length>];
<data_type> <variable_name> [ ];
The syntax to instantiate an array is :-
<data_type> <variable_name> [ ] = new <data_type> [<array_length>];
Qus. - 04 : What is exception handling ?
Ans. :- Exception handling :- Exception handling is a mechanism is a mechanism that handles runtime errors, so that the normal program flow does not get interrupted.
Java implements exception handling using the try and catch keywords.
Java implements exception handling using the try and catch keywords.
Qus. - 05 : Explain the difference between the try and catch keywords.
Ans. :- 1. The try block contains statements that are expected to give exceptions, where as the
catch block is used to handle the exception defined within a try block.
catch block is used to handle the exception defined within a try block.
2. No arguments needed in try block, where as in catch block, we have to mention
the argument, i.e., the type of exception that the catch block can catch.
Lab Question :
Qus. - 01 : Write a program to display the menu to order a burger at a burger joint.
Ans :-
import java.util.Scanner;
public class Burger_Joint
{
public static void main(String args[])
{
int choice,qty,cost,totalPrice;
char ans;
int veg_burger_rate=35;;
int chicken_burger_rate=49;
int mutton_burger_rate=69;
Scanner sc=new Scanner(System.in);
do
{
System.out.println("1. VEG. BURGER (Rs. - 35) ");
System.out.println("2. CHICKEN BURGER (Rs. - 49) ");
System.out.println("3. MUTTON BURGER (Rs. - 69)");
System.out.println("4. EXIT ");
System.out.println("Enter Your Choice (1/2/3/4) : ");
choice=sc.nextInt();
if(choice==1)
{
System.out.print( "How many Veg. Burger ? :" );
qty = sc.nextInt( );
totalPrice=qty*veg_burger_rate;
System.out.println( "You have to Pay Rs. :" + totalPrice);
}
if(choice==2)
{
System.out.print( "How many Chicken Burger ? :" );
qty = sc.nextInt( );
totalPrice=qty*chicken_burger_rate;
System.out.println( "You have to Pay Rs. :" + totalPrice);
}
if(choice==3)
{
System.out.print( "How many Mutton Burger ? :" );
qty = sc.nextInt( );
totalPrice=qty*mutton_burger_rate;
System.out.println( "You have to Pay Rs. :" + totalPrice);
}
if(choice==4)
{
break;
}
System.out.println("Do You Want to Continue.... (y/n) : ");
ans=sc.next().charAt(0);
}while(ans=='y');
}
}
public class Burger_Joint
{
public static void main(String args[])
{
int choice,qty,cost,totalPrice;
char ans;
int veg_burger_rate=35;;
int chicken_burger_rate=49;
int mutton_burger_rate=69;
Scanner sc=new Scanner(System.in);
do
{
System.out.println("1. VEG. BURGER (Rs. - 35) ");
System.out.println("2. CHICKEN BURGER (Rs. - 49) ");
System.out.println("3. MUTTON BURGER (Rs. - 69)");
System.out.println("4. EXIT ");
System.out.println("Enter Your Choice (1/2/3/4) : ");
choice=sc.nextInt();
if(choice==1)
{
System.out.print( "How many Veg. Burger ? :" );
qty = sc.nextInt( );
totalPrice=qty*veg_burger_rate;
System.out.println( "You have to Pay Rs. :" + totalPrice);
}
if(choice==2)
{
System.out.print( "How many Chicken Burger ? :" );
qty = sc.nextInt( );
totalPrice=qty*chicken_burger_rate;
System.out.println( "You have to Pay Rs. :" + totalPrice);
}
if(choice==3)
{
System.out.print( "How many Mutton Burger ? :" );
qty = sc.nextInt( );
totalPrice=qty*mutton_burger_rate;
System.out.println( "You have to Pay Rs. :" + totalPrice);
}
if(choice==4)
{
break;
}
System.out.println("Do You Want to Continue.... (y/n) : ");
ans=sc.next().charAt(0);
}while(ans=='y');
}
}
Qus. - 02 : Write a program to include a user-defined function to calculate the simple interest.
Ans :-
import java.util.Scanner;
public class Simple_Interest
{
public static void main(String args[])
{
Simple_Interest s=new Simple_Interest();
s.calculate();
}
void calculate()
{
float p, r, t,si;
Scanner s = new Scanner(System.in);
System.out.print("Enter the Principal Amount : ");
p = s.nextFloat();
System.out.print("Enter the Rate of interest : ");
r = s.nextFloat();
System.out.print("Enter the Time period : ");
t = s.nextFloat();
si = (p * r * t) / 100;
System.out.print("The Simple Interest is : " + si);
}
}
Ans :-
import java.util.Scanner;
public class Simple_Interest
{
public static void main(String args[])
{
Simple_Interest s=new Simple_Interest();
s.calculate();
}
void calculate()
{
float p, r, t,si;
Scanner s = new Scanner(System.in);
System.out.print("Enter the Principal Amount : ");
p = s.nextFloat();
System.out.print("Enter the Rate of interest : ");
r = s.nextFloat();
System.out.print("Enter the Time period : ");
t = s.nextFloat();
si = (p * r * t) / 100;
System.out.print("The Simple Interest is : " + si);
}
}
Qus. - 03 : Write a program to calculate the percentage of a student using functions and exception handling.
Ans :-
import java.util.Scanner;
public class Percentage
{
public static void main(String[] args)
{
int totalScore, obtainedScore;
Scanner sc = new Scanner(System.in);
Percentage p=new Percentage();
System.out.println("Enter the total score: ");
totalScore = sc.nextInt();
System.out.println("Enter the score obtained: ");
obtainedScore = sc.nextInt();
p.percentage(totalScore,obtainedScore);
}
void percentage(int totalScore,int obtainedScore)
{
float percentage=0.0f;
try
{
percentage = (obtainedScore * 100/ totalScore);
}
catch(ArithmeticException e)
{
System.out.println("You could not divide any value with zero, try again ...!");
}
System.out.println("The percentage is = " + percentage + " %");
}
}
import java.util.Scanner;
public class Percentage
{
public static void main(String[] args)
{
int totalScore, obtainedScore;
Scanner sc = new Scanner(System.in);
Percentage p=new Percentage();
System.out.println("Enter the total score: ");
totalScore = sc.nextInt();
System.out.println("Enter the score obtained: ");
obtainedScore = sc.nextInt();
p.percentage(totalScore,obtainedScore);
}
void percentage(int totalScore,int obtainedScore)
{
float percentage=0.0f;
try
{
percentage = (obtainedScore * 100/ totalScore);
}
catch(ArithmeticException e)
{
System.out.println("You could not divide any value with zero, try again ...!");
}
System.out.println("The percentage is = " + percentage + " %");
}
}
0 comments:
Post a Comment