AmbitionBox

Discover Best Places to work in India

C# Programming

C# Programming Questions and Answers

Showing 1 - 9 of 9 questions

1

Type of Conversion in which compiler is unable to convert the datatype implicitly is ?

a

ushort to long

b

int to uint

c

ushort to long

d

byte to decimal

correct answer b

‘int’ is 32 bit signed integer whereas ‘uint’ is 32 bit unsigned integer .Range of int is larger than uint.So,compiler cannot implicitly convert from larger datatype to smaller datatype.

2

’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

3

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)

4

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)

5

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)

6

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.

7

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;

}

8

For the given set of code select the relevant solution for conversion of data type.

staticvoid Main(string[] args)

{

     int num1 =20000;

     int num2 =50000;

     long total;

     total = num1 + num2;

     Console.WriteLine("Total is : "+total);

     Console.ReadLine();

}

 

 

 

a

Compiler will generate runtime error

b

Conversion is implicit type, no error generation

c

Specifying datatype for conversion externally will solve the problem

d

None of the mentioned

correct answer b

Since,conversion of datatype is implicit type as ‘int’ is a subset of ‘longtype’ hence no need to explicitly convert data from one type to another.Compiler will automatically do conversion.
Output : Total is : 70000.

9

Predict the relevant output for the given set of code.

staticvoid Main(string[] args)

{

     float sum;

     int i;

     sum = 0.0F;

     for(i =1; i <=10; i++)

     {

          sum = sum +1/(float)i;

     }

     Console.WriteLine("sum ="+sum);

     Console.ReadLine();

}

 

 

 

 

 

a

2.000

b

2.910

c

2.928

d

3.000

correct answer c

Answer: Option (C)

Select a company to compare with

vs

Similar Companies