Programming Using Loops

Multiple Choice Questions :-

        1.  Which loop is used to execute the statement at least once ?
a.       for loop                                 b.     while loop
c.      do while loop                       d.     count loop
        2.  Which one of the following control structure implements repetition ?
a.       loops                                    b.     if-else
c.      break                                     d.      if-else if
         3.  Which statement is used to end the loop ?
a.       continue                                b.      break
c.     goto                                      d.      exit
         4.  Which one of the following options stores variable of the same data type ?
a.       while loop                            b.      do while loop
c.     for loop                                d.      arrays
         5.  Which one of the following options is also known as the post-test loop ?
a.      do while                               b.      while
c.     for                                        d.       break

True of False :-

        1.       Loops cannot be used to implement repetition.                                FALSE
        2.       The for loop is used to write a loop that you want to execute a specific number of times. TRUE 
        3.       A semicolon is used with the while loop.               FALSE
        4.       Increment is used as the first part in the parentheses of the for loop statement.               FALSE    5.       Arrays do not store variable of the same data type.         FALSE

Subjective Questions :

1. What are the different kinds of loops ? Discuss.
Ans :- There are mainly three kinds of loops :-
(i) while :- The while loop repeats a selection of code based on a given boolean expression.
                  Syntax :-
                                 while(boolean expression)
                                       {
                                        statement(s);
                                       }
                 The statements within the while loop execute till the time the result of the boolean
                 expressions remains true. The while loop stops executing when the result of the boolean
                 expression becomes false.
(ii) do while :- The do while loop repeats a section of a code based on a boolean expression similar to
                        the while loop. But, the do while loop evaluates the loop condition after executing the
                        statements at least once within the loop body.
                        Syntax :-
                                    do
                                       {
                                           statement(s);
                                       }while(boolean expression);
(iii) for :- The for loop repeats a section of code only a specific number of times.
                        Syntax :-
                                for(initialization ; boolean expression ; increment/decrement)
                                       {
                                        statement(s);
                                       }
2. Write the syntax of the while loop and do while loop.
    Ans :-
(i) Syntax of while loop :-
                  Syntax :-
                                 while(boolean expression)
                                       {
                                        statement(s);
                                       }
               
(ii) Syntax of do while loop :-
                        Syntax :-
                                    do
                                       {
                                           statement(s);
                                       }while(boolean expression);


3. Differentiate between the while loop and do while loop.
    Ans :-
   
4. Discuss the use of the break statement.
Ans :- The break statement is used to end the loop. When the break statement is encountered in the
           loop, the loop gets terminated and control goes to the next statement outside the loop.

5. What are arrays ? Discuss the advantages of arrays over the for loop.
  Ans :- Arrays are objects that store multiple values of the same
             data type in a computer memory location. 

     Advantages: 
             - It is used to represent multiple data items of same type by using only single name.


Lab Questions :

1. Write a program to find all even numbers between 1 to 50.
Ans :-

class EvenNumbers
{
    public static void main(String args[])
{         
int i;                           
System.out.println("Printing Even numbers between 1 to 50 :-");
for(i=2;i<50;i=i+2)
{
System.out.print(i + " ");
}
}
}
2. Write a program to print the first 10 natural numbers.
Ans :-

class NaturalNumber
{ public static void main(String[] args) { int i; System.out.println ("The first 10 natural numbers are:-"); for (i=1;i<=10;i++) { System.out.println (i); } } }

3. Write a program to store marks of five subjects in an array, and then calculate the average of  marks.
Ans. :-
import java.util.Scanner;
class Average
{
    public static void main(String args[])
    {
        int mark [ ] = new int[5];
        int i;
        float sum=0;
        float avg;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter marks Obtained in 5 Subjects : ");
        for(i=0; i<5; i++)
        {
            mark[i] = sc.nextInt();
            sum = sum + mark[i];
        }
        avg = sum/5;  
        System.out.print("Average Marks = " +avg);
    }
}

0 comments:

Post a Comment