AmbitionBox

Discover Best Places to work in India

C# Programming

C# Programming Questions and Answers

Showing 41 - 50 of 174 questions

41

Correct syntax for while statement is:

a

while
{

}(condition);

b

while(condition)
{

};

c

while(condition)
{

}

d

while(condition);
{

}

correct answer c

Answer: Option (C)

42

Select the output for the following set of Code:

staticvoid 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

correct answer c

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.

43

Select the output for the following set of code:

staticvoid 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

correct answer c

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.

44

Select the output for the following set of code:

staticvoid 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

correct answer b

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

45

’Implicit Conversion’ follows the order of conversion as per compatibility of datatype as :

a

float > char > int

b

char > int > float

c

int > char > float

d

float > int > char

correct answer b

Answer: Option B

46

What is the need for ‘Conversion of data type’ in C#?

a

To store a value of one data type into a variable of another data type

b

To get desired data

c

To prevent situations of run time error during change or conversion of data type

d

None of the mentioned

correct answer c

Answer: Option (C)

47

Types of ‘Data Conversion’ in C#?

a

Implicit Conversion

b

Explicit Conversion

c

Implicit Conversion and Explicit Conversion

d

None of the mentioned

correct answer b

Answer: Option (B)

48

Subset of ‘int’ datatype is :

a

long ,ulong, ushort

b

long, ulong, uint

c

long, float, double

d

long, float, ushort

correct answer c

Answer: Option (C)

49

For the given set of code, is conversion possible?

staticvoid Main(string[] args)

{

     int a =76;

     char b;

     b =(char)a;

     Console.WriteLine(b);

     Console.ReadLine();

}

 

a

Compiler will generate runtime error

b

Conversion is explicit type

c

Compiler will urge for conversion from ‘integer’ to ‘character’ datatype

d

None of the mentioned

correct answer b

Since, given conversion is of explicit type as one datatype is in integer and other is in ‘char’.Compiler is needed to make a clear distinction between both type of datatypes and hence,explicitly one need to specify datatype as compiler is unable to make automatic conversion.
Output : L.

50

Which of the conversions are valid for the given set of code?

staticvoid Main(string[] args)

{

     int a =22;

     long b =44;

     double c =1.406;

     b = a;

     c= a;

     a= b;

     b = c;

}

 

 

 

a

c = a, b = c

b

a = b, b = a

c

b = a, c = a

d

All of the mentioned

correct answer a,b

Explanation:Conversion of data type from ‘int’ to ‘double’ is implicit in nature for ‘c = a’ as int is subset of double but same is not applicable for ‘b = c’ as ‘c’ had wider scope of data range then ‘b’ so explicit conversion is needed. Same explanation for option ‘b’.
Output :
Error 1 :Cannot implicitly convert type ‘long’ to ‘int’. An explicit conversion exists (are you missing a cast?).
Error 2 :Cannot implicitly convert type ‘double’ to ‘long’. An explicit conversion exists (are you missing a cast?)

Correct solution :

static void Main(string[] args)

{

      int a = 22;

      long b = 44;

      double c = 1.406;

      b = a;

      c = a;

     a = (int)b;

     b = (long)c;

}

Select a company to compare with

vs

Similar Companies