C# MCQ Set 1
1. Which of these methods of class String is used to separate a substring from a String object?
a) substring()
b) Substring()
c) SubString()
d) None of the mentioned
Answer
Answer: b [Reason:] None.
2. What will be the output for the given set of code?
-
static void Main(string[] args)
-
{
-
String a = "Ilove";
-
String b = "CSHARP";
-
b = string.Concat(a, ' ', b);
-
Console.WriteLine(b);
-
Console.ReadLine();
-
}
a) IloveCSHARP
b) I loveCSHARP
c) Ilove
d) Ilove CSHARP
Answer
Answer: d [Reason:] Concat() method is used to join two strings without the use of ‘+’ operator .
Output : Ilove CSHARP
3. Which of these methods of class are used to remove the leading and backward whitespaces?
a) startsWith()
b) trim()
c) Trim()
d) doTrim()
Answer
Answer: c [Reason:] None.
4. What will be the output for the given set of code?
-
static void Main(string[] args)
-
{
-
String a = "Ilove";
-
String b = "CSHARP";
-
b = string.Concat(a,' ',b);
-
string d = b.TrimStart('I', 'l', 'o', 'H');
-
Console.WriteLine(d);
-
Console.ReadLine();
-
}
a) Ilove CSHARP
b) love CSHARP
c) ve CSHARP
d) ve CSARP
Answer
Answer: c [Reason:] trimstart() removes character mentioned consecutively in front positions not characters in mentioned in between positions.
Output : ve CSHARP
5. What will be the output for the given set of code?
-
static void Main(string[] args)
-
{
-
String c = " Hello Computer ";
-
String a = c.Trim();
-
Console.WriteLine(""" + s + """);
-
}
a) ” Hello Computer ”
b) “HelloComputer”
c) “Hello Computer”
d) Hello Computer
Answer
Answer: c [Reason:] Trim() method is used to remove forward and backward spaces in strings.
Output : “Hello Computer”
6. What will be the output for the given set of code?
-
static void Main(string[] args)
-
{
-
String c = "Hello";
-
String a = c + "Bye";
-
Console.WriteLine(a);
-
Console.ReadLine();
-
}
a) “Hello Bye”
b) “HelloBye”
c) Hello Bye
d) HelloBye
Answer
Answer: d [Reason:] ‘+’ operator method works in the form of concatenate method() and hence is used to join two strings together.
Output : HelloBye
7. What will be the output for the given set of code?
-
static void Main(string[] args)
-
{
-
String c = "Hello";
-
String a ;
-
a = c.Replace('l', 'w');
-
Console.WriteLine(a);
-
Console.ReadLine();
-
}
a) Helloll
b) Hewlo
c) Helwo
d) Hewwo
Answer
Answer: d [Reason:] None.
Output : Hewwo
8. Which of the following statements is correct?
a) replace() replace() method replaces last occurrence of a character in invoking strings with another character
b) replace() method replaces only first occurrence of a character in invoking strings with another character
c) replace() method replaces all occurrences of one character in invoking strings with another character
d) none of the mentioned
Answer
Answer: c [Reason:] By definition.
9. What will be the output of the given code snippet?
-
static void Main(string[] args)
-
{
-
String c = "Hello i love you";
-
String a ;
-
a = c.Substring(12, 3);
-
Console.WriteLine(a);
-
Console.ReadLine();
-
}
a) ove
b) you
c) yo
d) love you
Answer
Answer: c [Reason:] None.
Output : yo
10. Which among the following is the correct way to find out the index of second ‘s’ in the string “She sold her beauty in one night to someone else”?
-
a) String a = "She sold her beauty in one night to someone else";
-
int i;
-
i = a.SecondIndexOf("s");
-
b) String a = "She sold her beauty in one night to someone else";
-
int i, j;
-
i = a.FirstIndexOf("s");
-
j = a.IndexOf("s", i + 1);
-
c) String a = "She sold her beauty in one night to someone else";
-
int i, j;
-
i = a.IndexOf("s");
-
j = a.IndexOf("s", i + 1);
-
d) None of the mentioned
Answer
Answer: c [Reason:] static void Main(string[] args)
{
String c = “She sold her beauty in one night to someone else”;
int i,j;
i = c.IndexOf(“s”);
j = c.IndexOf(“s”, i + 1);
Console.WriteLine(i + ” ” + j);
Console.ReadLine();
}
Output : 4, 36
C# MCQ Set 2
1.Which of the following is a correct statement about the C#.NET code given below?
-
struct book
-
{
-
private String name;
-
private int pages;
-
private Single price;
-
}
-
book b = new book();
a) New structure can be inherited from struct book
b) When the program terminates, variable b will get garbage collected
c) The structure variable ‘b’ will be created on the stack
d) When the program terminates, variable b will get garbage collected
Answer
Answer: c [Reason:] None.
2. Which of the following is a correct statement about the C#.NET code given below?
-
class trial
-
{
-
int i;
-
float d;
-
}
-
struct sample
-
{
-
private int x;
-
private Single y;
-
private trial z;
-
}
-
sample s = new sample();
a) trial object referred by z is created on the stack
b) z is created on the heap
c) Both s and z will be created on the heap
d) s will be created on the stack
Answer
Answer: d [Reason:] None.
3. Choose the correct statement among the following which supports the fact that C# does not allow the creation of empty structures?
a) C#.NET supports creation of abstract user-defined data types using structures
b) By having empty structures,it would mean that the new data types have no data associated with, which does not make any sense in C#.NET
c) Basic reason to create structures is the inability to represent real life objects using standard data types offered by the language
d) All of the mentioned
Answer
Answer: d [Reason:] Basic definition of structures in C#.NET.
4. Choose the correct statement about structures as to why they are defined as value types but not reference types?
a) Since space required for structure variables is allocated on stack which is a form of memory that is automatically available when a variable to be used is in scope.
b) Structures generally are used to represent user defined data types that consists of small amount of data in them.Hence using stack for declaration of such variables is not a problem.
c) All of the mentioned
d) None of the mentioned
Answer
Answer: c [Reason:] None.
5. Choose the wrong statement about structures in C#.NET?
a) Structures can be declared within a procedure
b) Structures can implement an interface but they cannot inherit from another structure
c) Structure members cannot be declared as protected
d) A structure cannot be empty
Answer
Answer: a [Reason:] None.
6. The correct way to define a variable of type struct abc among the following is?
-
struct abc
-
{
-
public string name;
-
protected internal int age;
-
private Single sal;
-
}
a) abc e = new abc();
b) abc();
c) abc e;
e = new abc;
d) abc e = new abc;
Answer
Answer: a [Reason:] None.
7. Which of the following is the correct way to settle down values into the structure variable ‘e’ defined in the following code snippet?
-
struct emp
-
{
-
public String name;
-
public int age;
-
public Single sal;
-
}
-
emp e = new emp();
a)
-
e.name = "Ankit";
-
e.age = 24;
-
e.sal = 200;
b)
-
With emp e
-
{
-
.name = "Ankit";
-
.age = 24;
-
.sal = 200;
-
}
c)
-
name = "Ankit";
-
age = 24;
-
sal = 200;
d) All of the mentioned
Answer
Answer: a [Reason:] None.
8. When does a structure variable get destroyed?
a) When no reference refers to it,it will get garbage collected
b) Depends on whether it is created using new or without new operator
c) As variable goes out of the scope
d) Depends on either we free its memory using free() or delete()
Answer
Answer: c [Reason:] None.
9. Calculate the number of bytes a structure variable s occupies in the memory if it is defined as follows.
-
class abc
-
{
-
int i;
-
Decimal d;
-
}
-
struct sample
-
{
-
private int x;
-
private Single y;
-
private trial z;
-
}
-
sample s = new sample();
a) 24 bytes
b) 8 bytes
c) 16 bytes
d) 12 bytes
Answer
Answer: d [Reason:] None.
Output – 12 bytes
10. Which of the following is the correct output for the C#.NET program code given below?
-
{
-
struct abc
-
{
-
public int i;
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
sample a = new sample();
-
a.i = 10;
-
fun(ref a);
-
Console.WriteLine(a.i);
-
}
-
public static voidn fun(ref sample x)
-
{
-
x.i = 20;
-
Console.WriteLine(x.i);
-
}
-
}
-
}
a) 10
10
b) 20
10
c) 10
20
d) 20
20
Answer
Answer: d [Reason:] None.
Output – 20
20
11. Select the wrong statements among the following?
a) A structure can contain properties
b) A structure can contain constructors
c) A structure can contain protected data members
d) A structure can contain constants
Answer
Answer: c [Reason:] None.
12. Which of the following is the correct result for the given statement in the C#.NET statement given below?
p = q
-
struct employee
-
{
-
private int employee id;
-
private string city;
-
}
-
employee q = new employee();
-
employee p;
-
p = q;
a) Elements of ‘q’ will be copied into corresponding elements of p.
b) Address stored in q will get copied into p
c) Address of first element of q will get copied into p
d) Once assignment is over.q will go out of scope and hence get exited forever
Answer
Answer: a [Reason:] None.
13. Which of the following will be the correct output for the program given below?
-
{
-
struct abc
-
{
-
int i;
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
abc x = new abc();
-
abc z;
-
x.i = 10;
-
z = x;
-
z.i = 15;
-
console.Writeline(x.i + " " + y.i)
-
}
-
}
-
}
a) 10 10
b) 10 15
c) 15 10
d) 15 15
Answer
Answer: b [Reason:] None.
Output: 10 15
C# MCQ Set 3
1. Select the output for the following set of code.
-
static void Main(string[] args)
-
{
-
int movie = 1;
-
switch (movie << 2 + movie)
-
{
-
default:
-
Console.WriteLine("3 Idiots");
-
break;
-
case 4:
-
Console.WriteLine("Ghazini");
-
break;
-
case 5:
-
Console.WriteLine("Krishh");
-
break;
-
case 8:
-
Console.WriteLine("Race");
-
break;
-
}
-
Console.ReadLine();
-
}
a) 3 Idiots
b) Ghazini
c) Race
d) Krishh
Answer
Answer: c [Reason:] We can put ‘default’ case in any order and hence write cases in any order.
Output: Race.
2. Select the output for the following set of code :
-
static void Main(string[] args)
-
{
-
int i = 2, j = 4;
-
switch (i + j * 2)
-
{
-
case 1 :
-
case 2 :
-
Console.WriteLine("1 and 2");
-
break;
-
case 3 to 10:
-
Console.WriteLine("3 to 10");
-
break;
-
}
-
Console.ReadLine();
-
}
a) 3 to 10 will be printed
b) 1 and 2 will be printed
c) The code reports an error as missing ; before :
d) The code gives output as 3 to 10
Answer
Answer: c [Reason:] Syntax error- switch case does not work with syntax as 3 to 10:
Output :
static void Main(string[] args)
{
int i = 2,j = 4;
switch (i + j * 2)
{
case 1 :
case 2 :
Console.WriteLine(“1 and 2”);
break;
case 3 :
Console.WriteLine(“3 to 10”);
break;
}
Console.ReadLine();
}
Here i = 2,j = 4.So,(i + j * 2) gives output as 10 and case 10 is missing.So,prints nothing for given code.
3. Select the output for the following set of code :
-
static void Main(string[] args)
-
{
-
int i = 2, k = 3;
-
switch (i - k)
-
{
-
case -1:
-
++i;
-
++k;
-
break;
-
case 2:
-
--i;
-
++k;
-
break;
-
default:
-
i += 3;
-
k += i;
-
break;
-
}
-
Console.WriteLine(i + "n" + k);
-
Console.ReadLine();
-
}
a) 2 3 3
b) 3 2 3
c) 3 4 4
d) 5 10 10
Answer
Answer: c
Output: 3
4
4 [Reason:] i – k = -1.So, case -1 will be executed only.
4. Select output for the following set of code :
-
static void Main(string[] args)
-
{
-
int const p = 0;
-
switch (3 * 5 / 6)
-
{
-
case p:
-
Console.WriteLine("A");
-
break;
-
case p * 1:
-
Console.WriteLine("B");
-
break;
-
case p - 2:
-
Console.WriteLine("C");
-
break;
-
default:
-
Console.WriteLine("D");
-
}
-
}
a) A
b) B
c) C
d) Compile time error
Answer
Answer: d [Reason:] In case expression we don’t have constant variable.
5. Select output for the following set of code :
-
static void Main(string[] args)
-
{
-
int i = 2, j = 3, k = 4;
-
switch (i + j - k)
-
{
-
case 0: case 2: case 4:
-
++i;
-
k += j;
-
break;
-
case 1: case 3: case 5 :
-
--i;
-
k -= j;
-
break;
-
default:
-
i += j;
-
break;
-
}
-
Console.WriteLine(i + "n" + j + "n" + k);
-
Console.ReadLine();
-
}
a) 1 3 1
b) 2 3 4
c) 5 3 4
d) Compile time error.
Answer
Answer: a [Reason:] Solving expression (i + j – k) gives 1 and hence,solving for case 1:case 3:case 5:.
Output : 1
3
1
6. Select the output for the following set of code :
-
static void Main(string[] args)
-
{
-
int i = 9 , j = 7;
-
switch (i - j + 3)
-
{
-
case 9: 7:
-
j += 6;
-
break;
-
case 5:
-
i -= 4;
-
break;
-
}
-
Console.WriteLine(i + "n" + j);
-
Console.ReadLine();
-
}
a) 5 7
b) 9 13
c) Compile time error
d) 9 7
Answer
Answer: c [Reason:] Invalid expression’7:’ in case 9:7:.
7. Select the output for the following code :
-
static void Main(string[] args)
-
{
-
switch (5)
-
{
-
case 5.0f:
-
Console.WriteLine("harsh");
-
break;
-
case 5:
-
Console.WriteLine("amish");
-
break;
-
case 5.0L:
-
Console.WriteLine("ANKIT");
-
break;
-
default:
-
Console.WriteLine("ashish");
-
}
-
Console.ReadLine();
-
}
a) amish
b) ANKIT
c) harsh
d) Compile time error
Answer
Answer: d [Reason:] Only integral values are allowed for case expression.
5.0f = (int)5.0f.
5.0L =(int)5.0L.
8. Select output for the following code:
-
static void Main(string[] args)
-
{
-
int i;
-
int j = 1;
-
int []ar = {21, 22, 13, 4};
-
switch (ar[j])
-
{
-
case 1:
-
i++;
-
break;
-
case 2:
-
i += 2;
-
j = 3;
-
continue;
-
case 3:
-
i %= 2;
-
j = 4;
-
continue;
-
default:
-
--i;
-
}
-
Console.WriteLine(i);
-
Console.ReadLine();
-
}
a) 23
b) 15
c) Compile time error
d) 12
Answer
Answer: c [Reason:] Continue cannot be used as a part of switch statement.
Output :
static void Main(string[] args)
{
int i;
int j = 1;
int []ar = {21 , 22, 13, };
switch(ar[j])
{
case 1:
i++;
break;
case 2:
i += 2;
j = 3;
continue;
case 3:
i %= 2;
j = 4;
default:
–i;
}
Console.WriteLine(i);
Console.ReadLine();
}
9. Select the output for the following set of Code:
-
static void Main(string[] args)
-
{
-
char ch = Convert.ToChar('a' | 'b' | 'c');
-
switch (ch)
-
{
-
case 'A':
-
case 'a':
-
Console.WriteLine("case A|case a");
-
break;
-
case 'B':
-
case 'b':
-
Console.WriteLine("case B|case b");
-
break;
-
case 'C':
-
case 'c':
-
case 'D':
-
case 'd':
-
Console.WriteLine("case D|case d");
-
break;
-
}
-
Console.ReadLine();
-
}
a) Compile time error
b) case A|case a
c) case B|case b
d) case D|case d
Answer
Answer: d [Reason:] Case statement declared last will only be executed as no particular casenumber is declared is to be called.
Output : case D|case d
10. Select the output for the following set of Code:
-
static void Main(string[] args)
-
{
-
char ch = 'p';
-
switch (ch)
-
{
-
case 'p':
-
Console.WriteLine("coco" + "t" + Convert.ToInt32(ch));
-
break;
-
default:
-
Console.WriteLine("default");
-
break;
-
}
-
Console.WriteLine("main");
-
}
a) coco main
b) coco 112
c) coco 112 main
d) compile time error
Answer
Answer: c [Reason:] ASCII value of ‘p’ is 112.Hence, coco 112 main.
Output: coco 112 main.
C# MCQ Set 4
1. What is the use of try & catch?
a) It is used to manually handle the exception
b) It helps to fix the errors
c) It prevents automatic terminating of the program in cases when an exception occurs
d) All of the mentioned
Answer
Answer: d [Reason:] None.
2. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
-
try
-
{
-
int a = 9;
-
int b = 5;
-
int c = a / b - 5;
-
Console.WriteLine("Hello");
-
}
-
catch(Exception e)
-
{
-
Console.WriteLine("C");
-
}
-
finally
-
{
-
Console.WriteLine("sharp");
-
}
-
}
-
}
a) Hello
b) C
c) Hellosharp
d) Csharp
Answer
Answer: d [Reason:] finally block execution takes place after the tryblock, no matter exception is found or not. catch block is executed only when exception is found. Here divide by zero exception is found hence both catch and finally are executed.
3. Choose the statement which is incorrect?
a) try block does not need to be followed by catch block
b) try block can be followed by finally block instead of catch block
c) try can be followed by both catch and finally block
d) try need not to be followed by anything
Answer
Answer: d [Reason:] try followed by either catch or finally block.
4. What will be the output of the program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
try
-
{
-
int a = 10;
-
int b = 5;
-
int c = a / b - 5;
-
Console.WriteLine("Hi");
-
}
-
catch(Exception e)
-
{
-
Console.WriteLine("hello");
-
}
-
}
-
}
a) Hi
b) hello
c) Hihello
d) Compile time error
Answer
Answer: b [Reason:] None.
5. Which of the keywords are used for the block to be examined for exceptions?
a) try
b) catch
c) throw
d) check
Answer
Answer: a [Reason:] try is used for the block that needs to be checked for exception.
6. Which of these keywords are used for the block to handle the exceptions generated by try block?
a) try
b) catch
c) throw
d) check
Answer
Answer :b [Reason:] None.
7. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
try
-
{
-
int a = 5;
-
int b = 10;
-
int c = b / a - 5;
-
Console.WriteLine("Csharp");
-
}
-
}
-
}
a) Csharp
b) sharp
c) C
d) Compile time error
Answer
Answer: d [Reason:] try should be followed by either catch or finally.
8. What is the output of the given code snippet?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
try
-
{
-
int a = 0;
-
int b = 5;
-
int c = a / b - 5;
-
Console.WriteLine("C");
-
}
-
finally
-
{
-
Console.WriteLine("sharp");
-
}
-
}
-
}
a) C
b) sharp
c) Csharp
d) None of the mentioned
Answer
Answer: c [Reason:] finally block is always executed after try block, no matter if the exception is found or not.
9. What will be the output of the code snippet?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
try
-
{
-
int a = 10;
-
int b = 5;
-
int c = b - 5 / 5;
-
Console.WriteLine("Hi");
-
}
-
catch(Exception e)
-
{
-
Console.WriteLine("hello");
-
}
-
}
-
}
a) Hi
b) hello
c) Hihello
d) Compile time error
Answer
Answer: a [Reason:] None.
10. Which of these keywords are used for generating an exception manually?
a) try
b) catch
c) throw
d) check
Answer
Answer: c [Reason:] None.
C# MCQ Set 5
1. Select the output for the following set of code :
-
static void Main(string[] args)
-
{
-
int i, j;
-
for (i = 1; i <= 3; i++)
-
{
-
j = 1;
-
while (i % j == 2)
-
{
-
j++;
-
}
-
Console.WriteLine(i + " " + j);
-
}
-
Console.ReadLine();
-
}
a) 11 21 31
b) 1 12 13 1
c) 11 21 31
d) 1 1 2 1 3 1
Answer
Answer: c [Reason:] Since, condition never satisfied for any value of i and j for which (i % j == 2).Hence, j is always constant ‘1’ and ‘i’ increments for i = 1, 2, 3.
Output: 11 21 31.
2. Select the output for the following set of code:
-
static void Main(string[] args)
-
{
-
float s = 0.1f;
-
while (s <= 0.5f)
-
{
-
++s;
-
Console.WriteLine(s);
-
}
-
Console.ReadLine();
-
}
a) 0.1
b) 1.1
c) 0.1 0.2 0.3 0.4 0.5
d) No output
Answer
Answer: b [Reason:] For the first while condition check when s = 0. If it is true as control goes inside loop ++s increments value of s by 1 as 1+0.1 = 1.1. So, for next condition while loop fails and hence, prints final value of s as 1.1.
Output: 1.1
3. Select the output for the following set of Code:
-
static void Main(string[] args)
-
{
-
int i;
-
i = 0;
-
while (i++ < 5)
-
{
-
Console.WriteLine(i);
-
}
-
Console.WriteLine("n");
-
i = 0;
-
while ( ++i < 5)
-
{
-
Console.WriteLine(i);
-
}
-
Console.ReadLine();
-
}
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) 1 2 3 4 5
1 2 3 4 5
Answer
Answer: c [Reason:] For while(i++ < 5) current value of ‘i’ is checked first and hence prints incremented value afterwards.So, i =1, 2, 3, 4, 5.But, for while(++i < 5) current value is incremented first and then checks that value with given condition and hence then prints that value.So, i = 1, 2, 3, 4.
Output: 1 2 3 4 5
1 2 3 4
4. Select the output for the following set of Code:
-
static void Main(string[] args)
-
{
-
int x = 0;
-
while (x < 20)
-
{
-
while (x < 10)
-
{
-
if (x % 2 == 0)
-
{
-
Console.WriteLine(x);
-
}
-
x++;
-
}
-
}
-
Console.ReadLine();
-
}
a) 1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
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
Answer
Answer: c [Reason:] Inner while loop condition checks for even number between 0 an 10 and hence prints number between the given range.
Output: 0 2 4 6 8.
5. Predict the output for the following set of code :
-
static void Main(string[] args)
-
{
-
int x;
-
x = Convert.ToInt32(Console.ReadLine());
-
int c = 1;
-
while (c <= x)
-
{
-
if (c % 2 == 0)
-
{
-
Console.WriteLine("Execute While " + c + "t" + "time");
-
}
-
c++;
-
}
-
Console.ReadLine();
-
}
-
for x = 8.
a) Execute while 1 time
Execute while 3 time
Execute while 5 time
Execute while 7 time
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
Answer
Answer: b [Reason:] Checks condition if number is divisible by 2 then it will print it even number times as given for x = 8 so, prints between 2 to 8 times Similarly, for x = 5, Execute 2 and 4 time.
OUTPUT: Execute while 2 time.
Execute while 4 time.
Execute while 6 time.
Execute while 8 time.
6. Select the output for the following set of Code:
-
static void Main(string[] args)
-
{
-
int n, r;
-
n = Convert.ToInt32(Console.ReadLine());
-
while (n > 0)
-
{
-
r = n % 10;
-
n = n / 10;
-
Console.WriteLine(+r);
-
}
-
Console.ReadLine();
-
}
-
for n = 5432.
a) 3245
b) 2354
c) 2345
d) 5423
Answer
Answer: c [Reason:] Reverse of number using while loop.
Output: 2345.
7. Correct syntax for while statement is:
-
a) while
-
{
-
-
-
-
}(condition);
-
b) while(condition)
-
{
-
-
-
};
-
c) while(condition)
-
{
-
-
-
}
-
d) while(condition);
-
{
-
-
-
}
Answer
Answer:c [Reason:] By defination.
8. Select the output for the following set of Code:
-
static void Main(string[] args)
-
{
-
float i = 1.0f, j = 0.05f;
-
while (i < 2.0f && j <= 2.0f)
-
{
-
Console.WriteLine(i++ - ++j);
-
}
-
Console.ReadLine();
-
}
a) 0.05f
b) 1.50f
c) -0.04999995f
d) 1.50f
Answer
Answer: c [Reason:] for while(i = 1.0f and j = 0.05f). We, had ‘&&’ condition which gives ‘1’. So, control enters while loop. Since, i = 1 and i++ = first execute then increment. So, first with ‘i’ value as 1.0f and ++j = first increment and then executes we had j = 1.05f and Since operation (i++ – ++j) gives us a negative sign number. So, we can stick our choice to option ‘c’ clearly. Now, as i = 2.0f so loop breaks.
Output:-0.04999995f.
9. Select the output for the following set of code :
-
static void Main(string[] args)
-
{
-
int i = 0;
-
while (i <= 50)
-
{
-
if (i % 10 == 0)
-
continue;
-
else
-
break;
-
i += 10;
-
Console.WriteLine(i % 10);
-
}
-
}
a) code prints output as 0 0 0 0 0
b) code prints output as 10 20 30 40 50
c) infinite loop but donot print anything
d) Code generate error
Answer
Answer: c [Reason:] None.
10. Select the output for the following set of code:
-
static void Main(string[] args)
-
{
-
int i = 1, j = 1;
-
while (++i <= 10)
-
{
-
j++;
-
}
-
Console.WriteLine(i+ " " +j);
-
Console.ReadLine();
-
}
a) 12 11
b) 10 11
c) 11 10
d) 11 12
Answer
Answer: c [Reason:] As ++i, first increments then execute so, for ++i i is 11 and j++ is first execute then increments. So, j = 10.
Output:11 10.
11. Select the output for following set of Code :
-
static void Main(string[] args)
-
{
-
int i = 1;
-
while (i <= 1)
-
{
-
if ('A' < 'a')
-
{
-
Console.WriteLine("Hello...");
-
}
-
else
-
{
-
Console.WriteLine("Hi...");
-
}
-
i++;
-
}
-
Console.ReadLine();
-
}
a) Hi…
b) Hello….
c) Hi…infinite times
d) Hello infinite times
Answer
Answer: b [Reason:] Ascii value of ‘A’ is 65 and ‘a’ is 97.So, clearly ‘A’ < ‘a’.
Output:Hello.
12. Select the output for the following set of codes:
-
static void Main(string[] args)
-
{
-
int i = 0;
-
while (i++ != 0) ;
-
Console.WriteLine(i);
-
Console.ReadLine();
-
}
a) -127 to +127
b) 0 to 127
c) 1
d) Infinite loop condition
Answer
Answer: c [Reason:] i++ = first executes then increments as i = 0. So, i++ != 0, which is false clearly as i = 0. Now, control goes inside loop with i = 1. So, statement prints i = 1.
Output: 1.