2021
Fundamentals of
PROGRAMMING
Problems #3
PROGRAMMING Everywhere P1
Problems (3)
1. What will be the output of the following C# code?
1. static void Main(string[] args)
2. {
3. int i;
4. for (i = 0; ; )
5. {
6. Console.WriteLine("hello");
7. }
8. Console.ReadLine();
9. }
a) No output
b) hello
c) hello printed infinite times
d) Code will give error as expression syntax
2. What will be the output of the following C# code?
1. static void Main(string[] args)
2. {
3. float f;
4. for (f = 0.1f; f <= 0.5; f += 1)
5. Console.WriteLine( ++f );
6. Console.ReadLine();
7. }
a) 1.1
b) 0.1
c) 0.1 0.2 0.3 0.4 0.5
d) None of the mentioned
3. What will be the output of the following C# code?
1. static void Main(string[] args)
2. {
3. int I, X;
4. for (I = 1; I <= (9 % 2 + I); I++)
5. {
6. X = (I * 3 + I * 2) / I;
7. Console.WriteLine(X);
8. }
9. Console.ReadLine();
10. }
a) Output of code is 5 10
b) Output is 5 5 5 5
c) Print 5 infinite times
d) None of the mentioned
1 M.A.S
PROGRAMMING Everywhere P1
4. What will be the output of the following C# code?
1. static void Main(string[] args)
2. {
3. int I, J = 0;
4. for (I = 1; I < 10; ) ;
5. {
6. J = J + I;
7. I += 2;
8. }
9. Console.WriteLine("Sum of first 10 even numbers is:"+J);
10. Console.ReadLine();
11. }
a) 1 2 3 4 5 6 7 8 9
b) 25
c) 1
d) Run time error
5. What will be the output of the following C# code?
1. static void Main(string[] args)
2. {
3. int i, s = 0;
4. for (i = 1; i <= 10; s = s + i, i++);
5. {
6. Console.WriteLine(s);
7. }
8. Console.ReadLine();
9. }
a) Code report error
b) Code runs in infinite loop condition
c) Code gives output as 0 1 3 6 10 15 21 28 36 45
d) Code give output as 55
6. Which statement is correct among the mentioned statements?
i. The for loop works faster than a while loop
ii. for( ; ; )implements an infinite loop
a) Only i is correct
b) Only ii is correct
c) Both i and ii are correct
d) Both i and ii are incorrect
8. Which of the C# code should be added to get the following output?
2 M.A.S
PROGRAMMING Everywhere P1
1. * * * * *
2. * * * *
3. * * *
4. * *
5. *
6. static void Main(string[] args)
7. {
8. int i,j;
9. /* Add code here */
10.
11. }
a)
for (i = 0;i<= 4; i++)
{
for(j = 0;j <= 4; j++)
console.Write("*");
console.WriteLine("\n");
}
b)
for (i = 0;i <= 4; i++)
{
for(j = 4;j<= i; j--)
console.Write("*");
console.WriteLine("\n");
}
c)
for (i = 0;i<= 4; i++)
{
for (j = i;j<= 4; j++)
console.Write("*");
console.WriteLine("\n");
}
d)
for ( i = 0;i<= 4; i++)
{
for (j = 0;j<= i; j++)
console.Write("*");
console.WriteLine("\n");
}
9. Which of the following is not infinite loop?
a) for( ,’0′,)
b) for( ;’0′; )
3 M.A.S
PROGRAMMING Everywhere P1
c) for( ;’1′; )
d) for( ,’1′, )
10. What will be the output of the following C# code?
1. static void Main(string[] args)
2. {
3. int i, j;
4. for (i = 1, j = i; i <= 3 && j >= 0; i++, j--)
5. {
6. if (i == j)
7. continue;
8. else
9. Console.WriteLine(j);
10. }
11. Console.ReadLine();
12. }
a) i = 0, j = 1;
b) i = 1, j = 0;
c) j = 0;
d) None of the mentioned
12.Correct syntax for do while loop is :
A.
do;
statement;
}while (condition);
B.
do(condition)
statement;
}while;
C.
do
4 M.A.S
PROGRAMMING Everywhere P1
statement;
}while (condition)
D.
do
statement;
}while (condition);
13. What will be the output of the following C# code?
1. static void Main(string[] args)
2. {
3. int i;
4. i = 0;
5. while (i++ < 5)
6. {
7. Console.WriteLine(i);
8. }
9. Console.WriteLine("\n");
10. i = 0;
11. while ( ++i < 5)
12. {
13. Console.WriteLine(i);
14. }
15. Console.ReadLine();
16. }
a)
1 2 3 4
1 2 3 4 5
b)
1 2 3
1 2 3 4
c)
1 2 3 4 5
1 2 3 4
d)
5 M.A.S
PROGRAMMING Everywhere P1
1 2 3 4 5
1 2 3 4 5
14. What will be the output of the following C# code?
1. static void Main(string[] args)
2. {
3. int x = 0;
4. while (x < 20)
5. {
6. while (x < 10)
7. {
8. if (x % 2 == 0)
9. {
10. Console.WriteLine(x);
11. }
12. x++;
13. }
14. }
15. Console.ReadLine();
16. }
a) 1 2 3 4 5 6 7 8 9 10
b) 0 2 4 6 8 10 12 14 16 18 20
c) 0 2 4 6 8
d) 0 2 4 6 8 10
15. What will be the output of the following C# code?
1. static void Main(string[] args)
2. {
3. int x;
4. x = Convert.ToInt32(Console.ReadLine());
5. int c = 1;
6. while (c <= x)
7. {
8. if (c % 2 == 0)
9. {
10. Console.WriteLine("Execute While " + c + "\t" +
"time");
11. }
12. c++;
13. }
14. Console.ReadLine();
15. }
16. for x = 8.
a)
Execute while 1 time
Execute while 3 time
Execute while 5 time
Execute while 7 time
6 M.A.S
PROGRAMMING Everywhere P1
b)
Execute while 2 time
Execute while 4 time
Execute while 6 time
Execute while 8 time
c)
Execute while 1 time
Execute while 2 time
Execute while 3 time
Execute while 4 time
Execute while 5 time
Execute while 6 time
Execute while 7 time
d)
Execute while 2 time
Execute while 3 time
Execute while 4 time
Execute while 5 time
16. What will be the output of the following C# code?
1. static void Main(string[] args)
2. {
3. int n, r;
4. n = Convert.ToInt32(Console.ReadLine());
5. while (n > 0)
6. {
7. r = n % 10;
8. n = n / 10;
9. Console.WriteLine(+r);
10. }
11. Console.ReadLine();
12. }
13. for n = 5432.
a) 3245
b) 2354
c) 2345
d) 5423
17. What will be the output of the following C# code?
1. static void Main(string[] args)
2. {
3. float i = 1.0f, j = 0.05f;
4. while (i < 2.0f && j <= 2.0f)
5. {
6. Console.WriteLine(i++ - ++j);
7. }
8. Console.ReadLine();
7 M.A.S
PROGRAMMING Everywhere P1
9. }
a) 0.05f
b) 1.50f
c) -0.04999995f
d) 1.50f
18. What will be the output of the following C# code?
1. static void Main(string[] args)
2. {
3. int i = 0;
4. while (i <= 50)
5. {
6. if (i % 10 == 0)
7. continue;
8. else
9. break;
10. i += 10;
11. Console.WriteLine(i % 10);
12. }
13. }
a) code prints output as 0 0 0 0 0
b) code prints output as 10 20 30 40 50
c) infinite loop but doesn’t print anything
d) Code generate error
19. What will be the output of the following C# code?
1. static void Main(string[] args)
2. {
3. int i = 0;
4. while (i++ != 0) ;
5. Console.WriteLine(i);
6. Console.ReadLine();
7. }
a) -127 to +127
b) 0 to 127
c) 1
d) Infinite loop condition
20. What will be the output of the following C# code?
1. static void Main(string[] args)
2. {
3. float i = 1.0f, j = 0.05f;
4. do
5. {
6. Console.WriteLine(i++ - ++j);
7. }while (i < 2.0f && j <= 2.0f);
8. Console.ReadLine();
9. }
8 M.A.S
PROGRAMMING Everywhere P1
a) 0.05
b) -0.05
c) 0.95
d) -0.04999995
21. What will be the output of the following C# code?
1. static void Main(string[] args)
2. {
3. int i = 1, j = 5;
4. do
5. {
6. Console.WriteLine(i = i++ * j);
7. }while (i <= 10);
8. Console.ReadLine();
9. }
a) 5 10 15 20 25 30 35 40 45 50
b) 5 25
c) 5 11 16 21 26 31 36 41 46 51
d) 5 30
22. What will be the output of the following C# code?
1. static void Main(string[] args)
2. {
3. long x;
4. x = Convert.ToInt32(Console.ReadLine());
5. do
6. {
7. Console.WriteLine(x % 10);
8. }while ((x = x / 10) != 0);
9. Console.ReadLine();
10. }
11. enter x = 1234.
a) number of digits present in x
b) prints ‘1’
c) prints reverse of x
d) prints sum of digits of ‘x’
23. The C#.NET code snippet given below generates ____ numbers series as output?
int i = 1, j = 1, val;
while (i < 25)
{
Console.Write(j + " ");
val = i + j;
j = i;
i = val;
}
A.Prime
9 M.A.S
PROGRAMMING Everywhere P1
B. Fibonacci
C. Palindrome
D.Odd
E. Even
24. Select the output for the following set of code :
1. static void Main(string[] args)
2. {
3. int a = 0;
4. int i = 0;
5. int b;
6. for (i = 0; i < 5; i++)
7. {
8. a++;
9. Console.WriteLine("Hello \n");
10. continue;
11. }
12. Console.ReadLine();
13. }
a) print hello 4 times
b) print hello 3 times
c) print hello 5 times
d) print hello infinite times
11 M.A.S