C# Questions & Answers –Type Conversion in Expressions

This set of C# MCQs focuses on “Type Conversion in Expressions”.

1. 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 runtime error during change or conversion of data type
d) None of the mentioned
View Answer

Answer: c
Explanation: By Definition.

2. Types of ‘Data Conversion’ in C#?
a) Implicit Conversion
b) Explicit Conversion
c) Implicit Conversion and Explicit Conversion
d) None of the mentioned
View Answer

Answer: b
Explanation: By Definition.

3. ‘Implicit Conversion’ follows the order of conversion as per compatibility of data type as:
a) float < char < int
b) char < int < float
c) int < char < float
d) float < int < char
View Answer

Answer: b
Explanation: None.
advertisement
advertisement

4. For the following C# code select the relevant solution for conversion of data type.

  1.  static void Main(string[] args)
  2.  {
  3.      int num1 = 20000;
  4.      int num2 = 50000;
  5.      long total;
  6.      total = num1 + num2;
  7.      Console.WriteLine("Total is : " +total);
  8.      Console.ReadLine();
  9.  }

a) Compiler will generate runtime error
b) Conversion is implicit type, no error generation
c) Specifying data type for conversion externally will solve the problem
d) None of the mentioned
View Answer

Answer: b
Explanation: Since,conversion of data type 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.

5. The subset of ‘int’ data type is __________
a) long, ulong, ushort
b) long, ulong, uint
c) long, float, double
d) long, float, ushort
View Answer

Answer: c
Explanation: By definition.
advertisement

6. Type of Conversion in which compiler is unable to convert the data type implicitly is?
a) ushort to long
b) int to uint
c) ushort to long
d) byte to decimal
View Answer

Answer: b
Explanation: ‘int’ is 32 bit signed integer whereas ‘uint’ is 32 bit unsigned integer. Range of int is larger than uint. So, the compiler cannot implicitly convert from larger data type to smaller data type.
advertisement

7. Disadvantages of Explicit Conversion are?
a) Makes program memory heavier
b) Results in loss of data
c) Potentially Unsafe
d) None of the mentioned
View Answer

Answer: b
Explanation: By definition.

8. For the given set of C# code, is conversion possible?

  1.  static void Main(string[] args)
  2.  {
  3.      int a = 76;
  4.      char b;
  5.      b = (char)a;
  6.      Console.WriteLine(b);
  7.      Console.ReadLine();
  8.  }

a) Compiler will generate runtime error
b) Conversion is explicit type
c) Compiler will urge for conversion from ‘integer’ to ‘character’ data type
d) None of the mentioned
View Answer

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

 L.

9. What will be the output of the following C# code?

  1. static void Main(string[] args)
  2. {
  3.     float sum;
  4.     int i;
  5.     sum = 0.0F;
  6.     for (i = 1; i <= 10; i++)
  7.     {
  8.         sum = sum + 1 /(float)i;
  9.     }
  10.     Console.WriteLine("sum =" +sum);
  11.     Console.ReadLine();
  12. }

a) 2.000
b) 2.910
c) 2.928
d) 3.000
View Answer

Answer: c
Explanation: None.
Output :

 sum = 2.928698.

10. Which of the conversions are valid for the following C# code?

  1.  static void Main(string[] args)
  2.  {
  3.      int a = 22;
  4.      long b = 44;
  5.      double c = 1.406;
  6.      b = a;
  7.      c = a;
  8.      a = b;
  9.      b = c;
  10.  }

a) c = a, b = c
b) a = c, b = a
c) b = a, c = a
d) All of the mentioned
View Answer

Answer: c
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.
Output :

 Error 1: Can not 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;
 }

Sanfoundry Global Education & Learning Series – C# Programming Language.

To practice all areas of C# language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.