C# Programming

C# Programming Questions and Answers

Showing 1 - 10 of 174 questions

1

Correct Declaration of Values to variables ‘a’ and ‘b’?

a

int a = 32, b = 40.6

b

int a = 42; b = 40

c

int a = 32; int b = 40

d

int a = b = 42

correct answer c

a) Although,declaration of ‘b’ and ‘a’ are correct but initialization of value to ‘b’ should be ‘int’ datatype not float.
b) Missing declaration type of ‘b’.
c) correctly declared datatypes ‘a’ and ‘b’.
d) ‘b’ can’t be assigned values before declaration.

2

Select error in the given program :

1.

static Void Main(String[] args)

2.

 {

3.

 constint m =100;

4.

 int n =10;

5.

 constint k = n /5*100* n ;

6.

 Console.WriteLine(m * k);

7.

 Console.ReadLine();

8.

 }

 

 

a

‘k’ should not be declared constant

b

Expression assigned to ‘k’ should be constant in nature

c

Expression (m * k) is invalid

d

m ‘ is declared in invalid format

correct answer b

’k’ should be declared as const int k = 10/5 * 100*10 i.e only constant values should be assigned to a constant.
Output :Error 1 – The expression being assigned to ‘k’ must be constant.

3

Correct output for code is?

1.

 staticvoid Main(string[] args)

2.

 {

3.

 float a = 10.553f;

4.

 long b = 12L;

5.

 int c;

6.

 c = Convert.ToInt32(a + b);

7.

 Console.WriteLine(c);

8.

 }

 

 

 

 

a

23.453

b

22

c

23

d

22.453

correct answer c

The two datatype ‘float’ and ‘long’ after arithmetic operation completely converted to nearest whole number 23.
Output : 23.

4

Select the output for the relevant code set:

1.

 staticvoid Main(string[] args)

2.

 {

3.

 int a =4, b =5, c =7, u =9;

4.

 int h;

5.

 h =(Convert.ToInt32(u < b))+(a + b--)+2;

6.

 Console.WriteLine(h);

7.

 Console.WriteLine(b);

8.

 Console.WriteLine(u < b);

9.

 }

a

12,15,0

b

11,4,False

c

11,15,0

d

12,4,False

correct answer b

Step 1: Convert.ToInt32(u < b)(Evaluate result as 9 < 5 which is false in nature.So, solution is converted from 'false' to '0').

Step 2: (a + b--) evaluated as 4 + 5 = 9 + 2 =11. Step 3: u < b evaluated as 'False' without being converted to '0'.

Output : 11 4 False

5

What is the output of the following code?

static void Main(string[] args)

{

     int a = 5 ;

     if(Convert.ToBoolean((.002f)-(0.1f)))

     Console.WriteLine("Sachin Tendulkar");

    elseif(a ==5)

     Console.WriteLine("Rahul Dravid");

     else

     Console.WriteLine("Ms Dhoni");

     Console.ReadLine();

}

 

 

 

a

Rahul Dravid

b

Sachin Tendulkar

c

MS Dhoni

d

Warning : Unrechable code

correct answer b

(0.002 – 0.1f) not equivalent to zero hence it is true. So,only first if clause will execute and print:Sachin Tendulkar on console.As,first condition is always true so no else if statement will be executed.
Output: Sachin Tendulkar

6

Select the correct ‘if statement’ to be filled in the given set of code :

static void Main(string [] args)

{

     int[]num ={50, 65, 56, 88, 43, 52};

 

     int even =0, odd =0;

     for(int i =0;i < num.Length;i++)

     {

          /*_________________*/

     }

     Console.WriteLine("Even Numbers:"+even);

     Console.WriteLine("Odd Numbers:"+odd);

     Console.ReadLine();

}

 

a

if ((num % 2) == 0)

{

even += 1;

}

else

{

odd += 1;

}

b

if((num * i) == 0)
{
even += 1;
}
else
{
odd += 1;
}

c

if(num[i] % 2 == 0)
{
even += 1;
}
else
{
odd += 1;
}

d

if(num[i] % 2 = 0)
{
even += 1;
}
else
{
odd += 1;
}

correct answer c

int []num = {50, 65, 56, 88, 43, 52};
int even = 0,odd = 0;
for (int i = 0 ;i < num.Length ;i++) { if (num[i] % 2 == 0) { even += 1; } else { odd += 1; } } Console.WriteLine("Even Numbers: " +even); Console.WriteLine("Odd Numbers: " +odd); Console.ReadLine()

7

Select a convenient declaration and initialization of a floating point number:

a

float somevariable = 12.502D

b

float somevariable = (Double) 12.502D

c

float somevariable = (float) 12.502D

d

float somevariable = (Decimal)12.502D

correct answer c

We cannot implicitly convert a “double” number directly to any other datatype.Here, its float we have to add the required datatype to number as :
float somevariable = (float)12.502D;
or
Double somevariable = (Double)12.502D;

8

Correct way to define a value 6.28 in a variable ‘a’ where value cannot be modified ?

a

#define a 6.28F

b

pi = 6.28F

c

const float pi = 6.28F

d

const float pi

pi = 6.28F

correct answer c

Const is a reserve keyword whenever they are declared with any variables.The value stored in that variable always remain fixed.Any modification done to change value of that variable results in error.Hence, options a, b and d are rejected because value is not declared fixed.Now, for value ‘c’ and ‘d’ only option ‘c’ is correct because for value ‘d’ while declaring a constant variable we need to provide a constant value too as provided in ‘c’. So, option ‘c’ is correct way of declaration of constant variable.

9

Why does a float variable stop incrementing at number ‘16777216’ in given code in C#?

float = 0 ;

while(true)

{

     a++;

     if ( a > 16777216 )

     break;

}

 

a

Sign and Exponent for ‘16777217’ is same as for ‘16777216’

b

Mantissa is different for ‘16777216’ and ‘16777217’

c

Sign and Exponent for ‘16777217’ is different from ‘16777216’

d

None of the mentioned

correct answer b

16777216 is exactly 224, and would be represented as 32-bit float like so:
sign = 0 (positive number)
exponent = 24 (stored as 24 + 127 = 151 = 10010111)
mantissa = . 0
As 32 bits floating-point representation: 0 10010111 00000000000000000000000
Therefore: Value = (+ 1) * 2 ^ 24 * (1. 0 + . 0) = 2 ^ 24 = 16777216
Now let’s look at the number 16777217, or exactly 224 + 1:
sign and exponent are the same.
Mantissa should have to be exactly 2-24 so that (+ 1) * 2 ^ 24 * (1. 0 + 2 ^-24) = 2 ^ 24 + 1 = 16777217 and here lies the actual problem . The mantissa cannot have the value 2-24 because it only has 23 bits, so the number 16777217 just cannot be represented with the accuracy of 32-bit floating points numbers.

10

Select the output for the following set of Code :

staticvoid Main(string[] args)

{

    int a =8, b =6, c =10;

    int d = a * c *2/ Convert.ToInt32(Math.Pow((c - b), 2));

    if(d ==(c = Convert.ToInt32(Math.Sqrt(a * a + b * b)))&& c ==10)

     {

         Console.WriteLine("figure is hypotenuse");

      }

      else

      {

         Console.WriteLine("figure is square");

      }

}

 

 

 

 

a

Figure is square

b

Figure is hypotenuse

c

False

d

None of the mentioned

correct answer a

Solving the expression for ‘c’ we get c==10 in if first condition as (c == Convert.ToInt32(Math.Sqrt(a * a + b * b))). The logical condition when d == (c = 10) suits here . Similarly, going for second condition where c ==10 as ‘&&’ operator exists between both given condition and at last both are evaluated to true as c == 10. So, only first statement is executed.
Output :Figure is square

SignUp/Login
1-Step Login | Get unlimited access!
This is the alert