Using Conditional Constructs

True or False :-

1      1.  In the if-else construct, statements included under the “else” code block will  be run only if the
             specified condition is true.           FALSE
2      2.  && is a logical operator that returns true if either of the two conditions is true.            FALSE
3      3.  The if-else if construct is used when more than two conditions are to be tested in a
             program.  TRUE
4      4.  If the condition specified in if statement evaluates to true, else block of code is  
x           executed.  FALSE
5      5.  Not (!) logical operator returns true if the operand on right evaluates to true.   FALSE

Match the following :-

Subjective Questions :

Qus. 1: What are conditional constructs ? Name the different types of conditional constructs.
Ans :-  Conditional construct are used to make a decisions based on a given boolean condition.
            It is of 4 types :-
                  (i) if             (ii) else- if              (iii) if - else if                (iv) nested if else

Qus. 2:  Give the syntax of a simple if-else construct.
Ans :-
   Syntax :-





r />













Qus. 3: Give examples of a Java program where the AND logical operator is used.
Ans. :-
import java.util.Scanner;
class Greatest
    {
        public static void main(String args[])  
            {  
               Scanner sc=new Scanner(System.in);     
               System.out.println("Enter First Number :");
               int num1=sc.nextInt();
               System.out.println("Enter Second Number :");
               int num2=sc.nextInt();
               System.out.println("Enter Third Number :");
               int num3=sc.nextInt();
               if(num1>num2 && num1>num3)
                {
                    System.out.println("First Number is Greatest");
                }
               else if(num2>num1 && num2>num3)
                {
                    System.out.println("Second Number is Greatest");
                }
               else
                {
                    System.out.println("Third Number is Greatest");
                }
            }
    }

Qus.- 4 : Differentiate between if-else and if-else if constructs.
Ans :-


 if - else
 if - else if
This construct is used for only two paths of execution.
This construct is used for more than two paths of execution.
This construct has only one condition. 
This construct has more than one (multiple) condition. 

Qus. 5: Give the syntax of a simple if-else if construct.
Ans :-
     Syntax :-

   

Lab Questions :

Qus.-1 : Write a Java program that prompts the user to enter the age of a person and then prints weather that person is illegible to vote or not. Create this program using if-else construct. (Hint : A person whose age is equal to and above 18 years is illegible to vote.)

Ans :-

import   java.util.Scanner;
class Vote
    {
        public static void main(String args[])  
            {  
               Scanner sc=new Scanner(System.in);     
               System.out.println("Enter the age of a Person :");
               int age=sc.nextInt();               
               if(age>=18)
                {
                    System.out.println("The Person is illegible to vote.");
                }               
               else
                {
                    System.out.println("The Person is not illegible to vote.");
                }
            }
    }

2. Write a Java program to display the grade of a student along with a message. The grade and the message to be displayed as per percentage are given below :
Percentage
Grade
Message
90-100
A+
Excellent Job !
80-90
A
Well done !
70-80
B
Satisfactory performance !
60-70
C
You can do better !
40-60
D
Performance need improvement !
Below 40
E
you failed. Study hard ! 
Create this program using the if-else if construct.

Qus.- 3: Write a Java program that prompts the user to enter three numbers and then prints                   the largest number. Create this program using if-else if construct.
Ans :-

import java.util.Scanner;
class Greatest
    {
        public static void main(String args[])  
            {  
               Scanner sc=new Scanner(System.in);     
               System.out.println("Enter three Number :");
               int num1=sc.nextInt();
               int num2=sc.nextInt();
               int num3=sc.nextInt();
               if(num1>num2 && num1>num3)
                {
                    System.out.println("Largest Number="+num1);
                }
               else if(num2>num1 && num2>num3)
                {
                    System.out.println("Largest Number="+num2);
                }
               else
                {
                    System.out.println("Largest Number="+num3);
                }
            }
    }



0 comments:

Post a Comment