Search Placementyogi.com
 

C# Data Types - Advanced Objective Questions and Answers with Explanation

1

What would be the output of C# program given below?

using System;
namespace PlacementYogi
{
    class DataTypes
    {
        static void Main(string[] args)
        {
            int a = 1;
            float b = 1.1f;
            short c = 2;
            Console.WriteLine((float)a + b * c - (a += (short)b));
        }
    }
}
A.

1.2

B.

1.1

C.

1.0

D.

None of the above

See Answer & Explanation Lets Discuss
Correct answer is : A
Explanation

Let's break the expression and evaluate one by one:

  1. (float)a will become 1.0 (int type will be promoted to float)
  2. b * c will become 2.2 (short will be promoted to float thus b * c = 1.1 * 2.0 = 2.2
  3. a += (short)b expanded as a = a + (short)b will become 2 (short will be promoted to int so (short)b = 1)
Now results from points 1, 2 and 3 putting together as 1.0 +2.2 - 2 = 1.0 + 2.2 - 2.0 = 1.2
Note: In final evaluation 2 (int type) is promoted to float type thus becomes 2.0 
 
2

What would be the output of the following program?

using System;
namespace PlacementYogi
{
    class HexChar
    {
        static void Main(string[] args)
        {
            char c= '\u0061';
            Console.WriteLine(c);
        }
    }
}
A.

'\u0061'

B.

a

C.

97

D.

Compilation Error

See Answer & Explanation Lets Discuss
Correct answer is : B
Explanation

char c='\u0061' is the way to enter the value of a character in hexadecimal.

'\u' is the escape sequence to tell the compiler that the next 4 digits represent a hexadecimal number.

The Hexadecimal number is converted in to decimal and then the character equivalent of that unicode  number is printed.

0x0061 in decimal means 97 in decimal and the unicode representation of 97 is character 'a' .

 
3

Which of the following lines perform Automatic Type Conversion? (Choose all that apply)

using System;
namespace PlacementYogi
{
    class AutoConvertion
    {
        static void Main(string[] args)
        {
            byte b=10;  //line8
            short s=20; //line9
            int i;
            i=b;        //line11
            Console.WriteLine("i=" +i);
            i=s;        //line13
            Console.WriteLine("i=" +i);
        }
    }
}
A.

Line8

B.

Line9

C.

Line11

D.

Line13

See Answer & Explanation Lets Discuss
Correct answer is : A,B,C,D
Explanation

C# performs an automatic type conversion when storing a literal integer constant into variables of type byte, short, long and char.

Thus, All the options are correct.

In the line8 and line9 the integer constant 10 and 20 are assigned to byte and short respectively. Here Automatic Type Conversion occurs.

 In the line11 and line13 "Widening" happens i.e. byte and short are widened/ typecast into a larger datatype i.e. int.

 
 

Banner to LogicGuns.com

Hide Page Information

Data types Objective Questions and Answers with Explanation in C#, Advanced Interview Questions for Integral types, decimal, floating-point and string types

Here you can find advanced C# data types interview questions with answers and explanation.

Why Data Types in C#?

In this section you can learn and practice Data types (Questions with Answers) to improve your skills in order to face the interview, competitive examination and various entrance test (CAT, GATE, GRE, MAT, Bank Exam etc.) with full confidence.

Where can I get C# Data Types Questions and Answers with Explanation?

PlacementYogi provides you lots of fully solved advanced Data Types questions and answers with explanation. Fully solved examples with detailed answer description, explanation are given and it would be easy to understand. All students, fresher can download Data Types quiz questions with answers as PDF files and eBooks.

Where can I get C# Data Types Interview Questions and Answers (objective type, multiple choice)?

Here you can find objective Data Types questions and answers for interview and entrance examination. Multiple choice and true or false type and yes or no type questions are also provided.