Tutorial 2 1.State whether the following are true or false. a) A switch statement can always be replaced by a series of if else statement:True b)The break statement is an optional in the default case when it is the last one:True c)A loop construct may be implemented using if and go to statement:True d)It is legal to have negative increment in for statement:true e)The body of a do-while statement is always executed atleast once:true 2.Find the errors a) if (x+y=z && y>0) Ans: if (x+y==z && y>0) b) if (p<0) || (q<0) Ans:if((p<0) || (q<0)) 3a) if ( code==1) Console.WriteLine(“pass”); else Console.WriteLine(“Fail”); Ans:No error b) if(code>1) a=b+c; else a=0; Ans:No error 4 Find errors if any each of the following looping statements. Assume that all variables have been declared and assigned values. a) count=1; sum=sum+x; count=count+1; Ans:No error b) name=0; do { name=name+1; Console.WriteLine(“India \n”); }while (name=1); Ans:while(name==1);
c) for(x=1;x>10;x=x+1) { ………. ………. } Ans:No error d) m=1; n=0; for( ,m+n<19;++n) Console.WriteLine(“Hello\n”); m=m+10; Ans:for( ;m+n<19;++n) 5.What is the output of the following code? int m=100; while(true) { if(m<10); break; m=m-10; } Console.Writeline(“m is”+m); Ans:120 6.What is the output for the following. int m=100; int n=300; while(++m<-n) Console.WriteLine(m); Ans:Control will not enter to loop since (101<-300) is not true. 7.Write a program to compute the sum of digits of a given integer number? Using system; Class sum { public staic void Main() { string n=Console.ReadLine(); int m,sum=0; while(n!=0) {
n=n/10; m=n%10; sum=sum+m; } Console.WriteLine(“Sum of digits”+sum); } } 8.Write a program to print full output using for loop 1 2 2 3 3 3 4 4 4 4 5 5 5 5 Ans: using system; class format { public static void Main() { int i,j; for(i=1;i<=5;i++) { Console.WriteLine(“\t”); for(j=1;j<=i;j++) { Console.Write(“\t”+i); } } } 9What is wrong with the fullcode? jnt j=0; While(j<10) { j++; if(j==5) continue loop; Console.WriteLine(“j is “+J); } Ans:Continue statement should not have lable. 10.Give the syntax for ternary operator. Ans:(condition)?statement1:statement2;
5