tutorial5 noura by EQl2Wvp

VIEWS: 7 PAGES: 3

									                                            KING SAUD UNIVERSITY
                               COLLEGE OF COMPUTER AND INFORMATION SCIENCES
                                   INFORMATION TECHNOLOGY DEPARTMENT
CSC 111: Introduction to Programming with Java   Tutorial #5          1st Semester 1433-1434

    Q1. What is the output of the following Java programs?

        a) public class Program{
               public static void main(String[] args){
                   int n = -7;
                   while ((n % 2) == -1)
                   {
                      if (n == 2)
                          break;
                      n = n + 2;
                      System.out.println("n = " + n);
                   }
                }
        }


        b)public class Program{
               public static void main(String[] args){
                  for (int i=1;i<=5;i++)
                   { for (int j=1;j<=(5-i);j++)
                    {
                        System.out.print(" ");
                      }
                        for (int k=1;k<=i;k++)
                      {
                          System.out.print(i);
                        }

                           System.out.println();
                      }
             }
        }

        c)public class Program
        {
            public static void main(String[] args)
            {
              for(int i=10;i<100;i+=10)
              {if(i==30)
                continue;
               System.out.print(i+" ");
              if(i==80)
                break;
              }}}


 Ch2: String / input and output
                                            KING SAUD UNIVERSITY
                               COLLEGE OF COMPUTER AND INFORMATION SCIENCES
                                   INFORMATION TECHNOLOGY DEPARTMENT
CSC 111: Introduction to Programming with Java   Tutorial #5              1st Semester 1433-1434

    Q2. How many times will the following code print "Welcome to Java"?
    a) int count = 0;
    do {
       System.out.println("Welcome to Java");
    } while (++count < 10);

    b) int count = 0;
    do {    System.out.println("Welcome to Java");
    } while (count++ < 10);


    Q3 . Determine how many times the body of the loop is executed.

a) int x=1;
do{
x = x*2;
} while(x>=8);

b) int balance = 10;
while (true) {
  if (balance < 9) break;
  balance = balance - 9;
}

        Q4.Find the errors in the following programs and correct them:
    a. public static void main( String args[] )
         {
              int count=0;
             while(count<10)
             {
              if(count==5)
               continue;
             count++;
             System.out.println(count);
             }
         }


    b. public static void main( String args[] )
        {
              int count=10;
             do
             {

 Ch2: String / input and output
                                            KING SAUD UNIVERSITY
                               COLLEGE OF COMPUTER AND INFORMATION SCIENCES
                                   INFORMATION TECHNOLOGY DEPARTMENT
CSC 111: Introduction to Programming with Java   Tutorial #5          1st Semester 1433-1434

              count++;
              System.out.println(count);
             }while( count>0)
        }


    Q5. Rewrite the following:

a) using for loop

public static void main(String[] args){
  Scanner input=new Scanner (System.in);
  System.out.println("Enter your number");
  int n=input.nextInt();
  while(n!=-1)
  {
    System.out.println(n);
    System.out.println("Enter your number");
    n=input.nextInt();
  }
 }

b) using continue:

public static void main(String[] args)
   {
     Scanner input=new Scanner (System.in);
     int number;
     int sum=0;
     for(int i=1;i<=10;i++)
     {
     System.out.println("iteration number: "+i);

      System.out.println("Enter your number");
      number=input.nextInt();
      if(number>0)
      sum+=number;

      }
      System.out.print("the sum"+sum);                   }



 Ch2: String / input and output

								
To top