C# MCQ Set 1
1. Which of the classes provide the operation of reading from and writing to the console in C#.NET?
a) System.Array
b) System.Output
c) System.ReadLine
d) System.Console
Answer
Answer: d [Reason:] The method for reading and writing to the console in C#.NET is provided by System.Console class.This class gives us access to the standard input,output and standard error streams.
2. Which of the given stream methods provide access to the output console by default in C#.NET?
a) Console.In
b) Console.Out
c) Console.Error
d) All of the mentioned
Answer
Answer: b [Reason:] The standard output stream Console.Out sends output to the screen by default.
3. Which of the given stream methods provide the access to the input console in C#.NET?
a) Console.Out
b) Console.Error
c) Console.In
d) All of the mentioned
Answer
Answer: c [Reason:] Console.In is an instance of TextReader, and we can use the methods and properties defined by TextReader to access it to read the input from the keyboard.
4. The number of input methods defined by the stream method Console.In in C#.NET is?
a) 4
b) 3
c) 2
d) 1
Answer
Answer: b [Reason:] Two basic methods : read() and readline() and third method readkey() introduced in .NET FrameWork 2.0.
5. Select the correct methodS provided by Console.In?
a) Read(), ReadLine()
b) ReadKey(), ReadLine()
c) Read(), ReadLine(), ReadKey()
d) ReadKey(), ReadLine()
Answer
Answer: c [Reason:] The two method Read() and ReadLine() available in .NET Framework 1.0 and Third method ReadKey() was added by .NET Framework 2.0.
6. Choose the output returned when read() reads the character from the console?
a) String
b) Char
c) Integer
d) Boolean
Answer
Answer: c [Reason:] Read() returns the character read from the console. It returns the result. The character is returned as an int, which should be cast to char.
7. Choose the output returned when error condition is generated while read() reads from the console.
a) False
b) 0
c) -1
d) All of the mentioned
Answer
Answer: c [Reason:] Read() returns –1 on error. This method also throws an IOException on failure.
8. Choose the object of TextReader class.
a) Console.In
b) Console.Out
c) Console.Error
d) None of the mentioned
Answer
Answer: a [Reason:] Console.In is an instance(object) of TextReader class and we can use the methods and properties defined by TextReader to invoke the object console.in.
9. Choose the object/objects defined by the Textwriter class.
a) Console.In
b) Console
c) Console.Error
d) None of the mentioned
Answer
Answer: c [Reason:] Console.Out and Console.Error are objects of type TextWriter class.
10. Choose the output for the given set of code:
-
static void Main(string[] args)
-
{
-
int a = 10, b = 0;
-
int result;
-
Console.Out.WriteLine("This will generate an exception.");
-
try
-
{
-
result = a / b; // generate an exception
-
}
-
catch (DivideByZeroException exc)
-
{
-
Console.Error.WriteLine(exc.Message);
-
}
-
Console.ReadLine();
-
}
a) This will generate an exception
b) 0
c) Compile time error
d) This will generate an exception
Attempted to Divide by Zero
Answer
Answer: d [Reason:] None.
11. Choose the methods provided by Console.Out and Console.Error?
a) Write
b) WriteLine
c) WriteKey
d) Write & WriteLine
Answer
Answer: d [Reason:] None.
12. Choose the output for the following set of code?
-
static void Main(string[] args)
-
{
-
Console.WriteLine("This is a Console Application:");
-
Console.Write("Please enter your lucky number:");
-
string val1 = Console.ReadLine();
-
int val2 = System.Convert.ToInt32(val1, 10);
-
val2 = val2 * val2;
-
Console.WriteLine("square of number is:" +val2);
-
Console.Read();
-
}
a) Compile time error
b) Runs successfully does not print anything
c) Runs successfully, ask for input and hence displays the result
d) Syntax Error
Answer
Answer: c [Reason:] None.
Output : This is a Console Application:
Please enter your lucky number: 3
Square of number is : 9
C# MCQ Set 2
1. The correct way to implement a read only property add, in a math class is?
a)
-
class math
-
{
-
int ad;
-
public int add
-
{
-
get
-
{
-
return ad;
-
}
-
}
-
-
}
b)
-
class math
-
{
-
public int add
-
{
-
get
-
{
-
return ad;
-
}
-
}
-
}
c)
-
class math
-
{
-
int ad;
-
public int add
-
{
-
get
-
{
-
return ad;
-
}
-
set
-
{
-
ad = value;
-
}
-
}
-
}
d) None of the mentioned
Answer
Answer: a [Reason:] None.
2. The correct way to implement a write only property add in a math class?
a)
-
class math
-
{
-
public int add
-
{
-
set
-
{
-
add = value;
-
}
-
}
-
}
b)
-
class math
-
{
-
int ad;
-
public int add
-
{
-
set
-
{
-
ad = value;
-
}
-
}
-
}
c)
-
class math
-
{
-
int ad;
-
public int add
-
{
-
get
-
{
-
return ad;
-
}
-
set
-
{
-
ad = value;
-
}
-
}
-
}
d) None of the mentioned
Answer
Answer: b [Reason:] None.
3. Select the correct statement about properties of read and write in C#.NET?
a) A property can simultaneously be read or write only
b) A property cannot be either read only or write only
c) A write only property will only have get accessor
d) A read only property will only have get accessor
Answer
Answer: d [Reason:] None.
4. What will be the output of following snippet of code?
-
class number
-
{
-
private int num1;
-
private int num2;
-
public int anumber
-
{
-
get
-
{
-
return num1;
-
}
-
set
-
{
-
num1 = value;
-
}
-
}
-
public int anumber1
-
{
-
get
-
{
-
return num2;
-
}
-
set
-
{
-
num2 = value;
-
}
-
}
-
}
-
class Program
-
{
-
public static void Main(string[] args)
-
{
-
number p = new number();
-
p.anumber = 20;
-
number k = new number();
-
k.anumber1 = 40;
-
int m = p.anumber;
-
int t = k.anumber1;
-
int r = p.anumber + k.anumber1;
-
Console.WriteLine("number = " +m);
-
Console.WriteLine("number = " +t);
-
Console.WriteLine("sum = " +r);
-
Console.ReadLine();
-
}
-
}
a) 0
b) Compile time error
c) 60
d) None of the mentioned
Answer
Answer: c [Reason:] None.
Output : 60
5. What will be the output of the following snippet of code?
-
class number
-
{
-
private int num1 = 60;
-
private int num2 = 20;
-
public int anumber
-
{
-
get
-
{
-
return num1;
-
}
-
set
-
{
-
num1 = value;
-
}
-
}
-
public int anumber1
-
{
-
get
-
{
-
return num2;
-
}
-
set
-
{
-
num2 = value;
-
}
-
}
-
}
-
class Program
-
{
-
public static void Main(string[] args)
-
{
-
number p = new number();
-
number k = new number();
-
int m = p.anumber;
-
int t = k.anumber1;
-
int r = p.anumber * k.anumber1;
-
Console.WriteLine("sum = " + r);
-
Console.ReadLine();
-
}
-
}
a) 0
b) 120
c) 1200
d) Compile time error
Answer
Answer: c [Reason:] None.
Output : 1200
6. What will be the output of the following snippet of code?
-
class number
-
{
-
int length = 50;
-
public int number1
-
{
-
get
-
{
-
return length;
-
}
-
set
-
{
-
length = value;
-
}
-
}
-
}
-
class Program
-
{
-
public static void Main(string[] args)
-
{
-
number p = new number();
-
p.number1 = p.number1 + 40;
-
int k = p.number1 * 3 / 9;
-
Console.WriteLine(k);
-
Console.ReadLine();
-
}
-
}
a) 0
b) 180
c) 30
d) Compile time error
Answer
Answer: c [Reason:] None.
Output : 30
7. What will be the output of the following snippet of code?
-
class number
-
{
-
int length = 60;
-
public int number1
-
{
-
get
-
{
-
return length;
-
}
-
}
-
}
-
class Program
-
{
-
public static void Main(string[] args)
-
{
-
number p = new number();
-
int l;
-
l = p.number1 + 40;
-
int k = l * 3 / 4;
-
Console.WriteLine(k);
-
Console.ReadLine();
-
}
-
}
a) 30
b) 75
c) 80
d) 0
Answer
Answer: b [Reason:] None.
Output : 75
8. What will be the output of the following snippet of code?
-
class student
-
{
-
int []scores = new int[5] {23, 42, 54, 11, 65};
-
public int this[int index]
-
{
-
get
-
{
-
if (index < 5)
-
return scores[index];
-
else
-
{
-
Console.WriteLine("invalid index");
-
return 0;
-
}
-
}
-
set
-
{
-
if (index < 5)
-
scores[index] = value;
-
else
-
Console.WriteLine("invalid index");
-
}
-
}
-
}
-
class Program
-
{
-
public static void Main(string[] args)
-
{
-
student s = new student();
-
Console.WriteLine(s[4] + 8);
-
Console.ReadLine();
-
}
-
}
a) 73
b) 37
c) 0
d) Run time error
Answer
Answer: a [Reason:] None.
Output : 73
9. The correct way to implement the property for which property reports the error “invalid index” if user attempts to cross bounds of the array for a student class with 5 intger arrays.
a)
-
class student
-
{
-
int []scores = new int[5] {23, 42, 54, 11, 65};
-
public int this[int index]
-
{
-
get
-
{
-
if (index < 5)
-
return scores[index];
-
else
-
{
-
Console.WriteLine("invalid index");
-
return 0;
-
}
-
}
-
set
-
{
-
if (index < 5)
-
scores[index] = value;
-
else
-
Console.WriteLine("invalid index");
-
}
-
}
-
}
b)
-
class student
-
{
-
int []scores = new int[5] {23, 42, 54, 11, 65};
-
public int this[int index]
-
{
-
get
-
{
-
if (index < 5)
-
return scores[index];
-
}
-
}
-
}
c)
-
class student
-
{
-
int []scores = new int[5]{23, 42, 54, 11, 65};
-
public int this[int index]
-
{
-
set
-
{
-
if (index < 5)
-
return scores[index];
-
else
-
{
-
Console.WriteLine("invalid index");
-
return 0;
-
}
-
}
-
}
-
}
d) None of the mentioned
Answer
Answer: a [Reason:] None.
10. What will be the output of the following snippet of code?
-
class student
-
{
-
int []scores = new int[3] {13, 32, 24};
-
public int this[int index]
-
{
-
get
-
{
-
if (index < 3)
-
return scores[index];
-
else
-
{
-
Console.WriteLine("invalid index");
-
return 0;
-
}
-
}
-
private set
-
{
-
if (index < 3)
-
scores[index] = value;
-
else
-
Console.WriteLine("invalid index");
-
}
-
}
-
}
-
class Program
-
{
-
public static void Main(string[] args)
-
{
-
student s = new student();
-
int[] scores1 = new int[3] {8, 19, 40};
-
for (int i = 0; i < 3; i++)
-
{
-
if (scores1[i] > s[i])
-
{
-
Console.WriteLine(" scores1 had greater value :" + scores1[i]);
-
}
-
else
-
{
-
Console.WriteLine("scores had greater value :" + s[i]);
-
}
-
}
-
Console.ReadLine();
-
}
-
}
a) 0
b) Compile time error
c) Run time error
d) scores had greater value : 13
scores had greater value : 32
scores1 had greater value : 40
Answer
Answer: d [Reason:] None.
Output : scores had greater value : 13
scores had greater value : 32
scores1 had greater value : 40
C# MCQ Set 3
1. The method in which large or variable number of arguments are handled is known as:
a) Value parameters
b) Output parameters
c) Parameter arrays
d) None of the mentioned
Answer
Answer: c [Reason:] None.
2. The modifiers used to define an array of parameters or list of arguments:
a) ref
b) out
c) param
d) var
Answer
Answer: c [Reason:] None.
3. What will be the output for the given set of code ?
-
static void Main(string[] args)
-
{
-
object[] a = {" 1 ", 4.0f, " harsh "};
-
fun(a);
-
Console.ReadLine();
-
}
-
static void fun(params object[] b)
-
{
-
for (int i = 0; i < b.Length - 1; i++)
-
Console.WriteLine(b[i] + " ");
-
}
a) 1 4.0 harsh
b) 1 4
c) 1 4 hars
d) 1 4 harsh
Answer
Answer: d [Reason:] ‘a’ is declared as array of objects which is passed as a parameter to a single method fun() using variable number of parameters.
Output : 1 4 harsh
4. Which of the following statements are correct?
a) C SHARP allows a function to have arguments with default values
b) C SHARP allows a function to have variable number of arguments
c) Params is used to specify the syntax for a function with variable number of arguments
d) Omitting the return value type in method definition results into an exception
Answer
Answer: b, c [Reason:] None.
5. What will be the output of the set of code?
-
static void Main(string[] args)
-
{
-
int [] a = {1, 2, 3, 4, 5};
-
fun(a);
-
Console.ReadLine();
-
}
-
static void fun(params int[] b )
-
{
-
int[] k = { 3, 4, 7, 8,'' };
-
for (int i = 0; i < b.Length; i++)
-
{
-
b[i] = b[i] + k[i] ;
-
Console.WriteLine( b[i] + " ");
-
}
-
}
a) Compile time error
b) 3, 4, 7, 8, 5
c) 3, 4, 7, 8, 5, 1, 2, 3, 4, 5
d) 4, 6, 10, 12, 5
Answer
Answer: d [Reason:] Passing of array parameters declared in main() and hence adding elements of array passed using param to another array k[] declared in fun() method.
Output : 4, 6, 10, 12, 5
6. What will be the output the of given set of code?
-
static void Main(string[] args)
-
{
-
int [] a = {1, 2, 3, 4, 5};
-
fun(a);
-
Console.ReadLine();
-
}
-
static void fun(params int[] b )
-
{
-
for (int i = 0; i < b.Length; i++)
-
{
-
b[i] = b[i] * 5 ;
-
Console.WriteLine(b[i] + "");
-
}
-
}
a) 1, 2, 3, 4, 5
b) 5, 10, 15, 20, 25
c) 5, 25, 125, 625, 3125
d) 6, 12, 18, 24, 30
Answer
Answer: b [Reason:] None.
Output : 5, 10, 15, 20, 25.
7. What will be the output of the given set of code?
-
static void Main(string[] args)
-
{
-
int[] a = { 2, 21, 34, 46, 85, 88, 90};
-
fun(a);
-
Console.WriteLine(a + " ");
-
Console.ReadLine();
-
}
-
static void fun(params int [] b )
-
{
-
int [] c = { 1, 2, 3, 4, 5, 6, 7};
-
int i ;
-
for (i = 0 ;i < b.Length ;i++)
-
if (b[i] % 2 == 0)
-
{
-
c[i] = b[i];
-
}
-
Console.WriteLine("even numbers are:");
-
for (i = 0 ;i <= b.Length ;i++)
-
{
-
Console.WriteLine(c[i]);
-
}
-
}
a) Compile time error
b) 2, 21, 34, 4, 6, 46, 88, 90
c) 2, 4, 34, 46, 6, 88, 90
d) 2, 34, 46, 88, 90
Answer
Answer: d [Reason:] None.
8. Select the correct declaration of the defining array of parameters:
a) void func(int[] x)
{
}
b) void func(int x)
{
}
c) void func(param int[])
{
}
d) void fun(param int[] x)
{
}
Answer
Answer: d [Reason:] None.
9. What will be the output of the given set of code?
-
static void Main(string[] args)
-
{
-
int[] x = { 80, 82, 65, 72, 83, 67 };
-
fun(x);
-
Console.ReadLine();
-
}
-
static void fun(params int [] b )
-
{
-
int i;
-
for (i = 5; i >=0 ; i--)
-
{
-
Console.WriteLine(Convert.ToChar(b[i]));
-
}
-
}
a) 67 83 72 65 82 80
b) P R A H S C
c) C S H A R P
d) 80 82 65 72 83 67
Answer
Answer: c [Reason:] None.
10. What will be the output of the given set of code?
-
static void Main(string[] args)
-
{
-
int[] x = {65, 66, 67, 68, 69, 70};
-
fun(x);
-
Console.ReadLine();
-
}
-
static void fun(params int[] b )
-
{
-
int i;
-
for (i = 5; i > 0 ; i--)
-
{
-
b[i] = b[i] + 32;
-
Console.WriteLine(Convert.ToChar(b[i]));
-
}
-
}
a) A, B, C, D, E, F
b) F, E, D, C, B, A
c) f, e, d, c, b
d) b, c, d, e, f
Answer
Answer: c [Reason:] None.
C# MCQ Set 4
1. What will be the output of given code snippet?
-
class UnsafeCode
-
{
-
struct MyStruct
-
{
-
public int a;
-
public int b;
-
public int Sum()
-
{
-
return a * b;
-
}
-
}
-
unsafe static void Main()
-
{
-
MyStruct o = new MyStruct();
-
MyStruct* p;
-
p = &o;
-
p->a = 10;
-
p->b = 20;
-
Console.WriteLine("Value is " + p->Sum());
-
Console.ReadLine();
-
}
-
}
a) Compile time error
b) Run time error
c) 200
d) 30
Answer
Answer: c [Reason:] A pointer can point to an object of a structure type as long as the structure does not contain reference types. When we access a member of a structure through a pointer, we must use the arrow operator, which is –>, rather than the dot (.) operator.
Output : 200
2. What will be the output of given code snippet?
-
class UnsafeCode
-
{
-
struct MyStruct
-
{
-
public int a;
-
public int b;
-
public int Sum()
-
{
-
return a / b;
-
}
-
}
-
unsafe static void Main()
-
{
-
MyStruct o = new MyStruct();
-
MyStruct* p;
-
p = &o;
-
p->a = 60;
-
p->b = 15;
-
int c = 30;
-
Console.WriteLine("Value is : " + p->Sum()*c);
-
Console.ReadLine();
-
}
-
}
a) Compile time error
b) 120
c) Run time error
d) 4
Answer
Answer: b [Reason:] None.
Output :120
3. What will be the output of given code snippet?
-
class UnsafeCode
-
{
-
unsafe static void Main()
-
{
-
int[] nums = new int[10];
-
fixed (int* p = &nums[0], p2 = nums)
-
{
-
if (p == p2)
-
Console.WriteLine("p and p2 point to same address.");
-
Console.ReadLine();
-
}
-
}
-
}
a) Run time error
b) Compile time error
c) p and p2 point to the same address
d) Only b
Answer
Answer: c [Reason:] None.
Output: p and p2 point to same address
4. What will be the output of given code snippet?
-
class UnsafeCode
-
{
-
static void Main()
-
{
-
int? count = null;
-
int? result = null;
-
int incr = 10;
-
result = count + incr;
-
if (result.HasValue)
-
Console.WriteLine("result has this value: " + result.Value);
-
else
-
Console.WriteLine("result has no value");
-
Console.ReadLine();
-
}
-
}
a) Run time error
b) 0
c) Result has no value
d) Compile time error
Answer
Answer: c [Reason:] A nullable object can be used in expressions that are valid for its underlying type.When non-nullable and nullable types are mixed in an operation, the outcome is a nullable value.
Output: result has no value
5. What will be the output of given code snippet?
-
class UnsafeCode
-
{
-
static void Main()
-
{
-
int count = 100;
-
int? result = null;
-
int incr = 10;
-
result = count + incr;
-
if (result.HasValue)
-
Console.WriteLine("result has this value: " + result.Value);
-
else
-
Console.WriteLine("result has no value");
-
Console.ReadLine();
-
}
-
}
a) Run time error
b) 110
c) Result has no value
d) Compile time error
Answer
Answer: b [Reason:] None.
Output: result has this value : 110
6. Choose the statement which defines the Nullable type Correctly:
a) A special version of a value type that is represented by a structure
b) A nullable type can also store the value null
c) Nullable types are objects of System.Nullable
d) All of the mentioned
Answer
Answer: d [Reason:] A nullable type is a special version of the value type that is represented by a structure. In addition to the values defined by the underlying type, a nullable type can also store the value null. Thus, a nullable type has the same range and characteristics as its underlying type. It simply adds the ability to represent a value which indicates that a variable of that type
is unassigned. Nullable types are objects of System.Nullable
7. What does the following code depicts?
1. System.Nullable
2. bool? done;
a) Code 1 declares the objects of nullable of type Nullable
b) Code 2 declares a nullable type in much shorter and in more commonly used way using ‘?’
c) Both a & b
d) None of the mentioned
Answer
Answer: c [Reason:] None.
8. Which operator is commonly used to find the size of the type of C#?
a) size()
b) sizeof(type)
c) both a & b
d) none of the mentioned
Answer
Answer: b [Reason:] None.
9. What will be the output of given code snippet?
-
unsafe struct FixedBankRecord
-
{
-
public fixed byte Name[80];
-
public double Balance;
-
public long ID;
-
}
-
class UnsafeCode
-
{
-
unsafe static void Main()
-
{
-
Console.WriteLine("Size of FixedBankRecord is " + sizeof(FixedBankRecord));
-
Console.ReadLine();
-
}
-
}
a) Run time error
b) 80
c) 96
d) Compile time error
Answer
Answer: c [Reason:] The purpose of a fixed-size buffer is to allow the creation of a struct in which the array of elements that make up the buffer are contained within the struct.By using a fixed-size buffer, we let the entire array to be contained within the struct.The overall size of FixedBankRecord is 96, which is the sum of its members.
Output : 96
10. What will be the output of given code snippet?
-
class UnsafeCode
-
{
-
unsafe static void Main()
-
{
-
int* ptrs = stackalloc int[3];
-
ptrs[0] = 1;
-
ptrs[1] = 2;
-
ptrs[2] = 3;
-
for (int i = 2; i >=0; i--)
-
Console.WriteLine(ptrs[i]);
-
Console.ReadLine();
-
}
-
}
a) 3 2 1
b) 1 2 3
c) None of the mentioned
d) Run time error
Answer
Answer: a [Reason:] Allocates memory from the stack by using stackalloc.Here, ptrs is a pointer that receives the address of the memory that is large enough to hold size of number of objects of type ‘int’.Here, type ‘int’ is a non reference type.Finally, stackalloc can be used only in an unsafe context.
Output : 3 2 1
C# MCQ Set 5
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 run time error during change or conversion of data type
d) None of the mentioned
Answer
Answer: c [Reason:] 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
Answer
Answer: b [Reason:] By Definition.
3.’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
Answer
Answer: b [Reason:] None.
4. For the given set of code select the relevant solution for conversion of data type.
-
static void 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
Answer
Answer: b [Reason:] 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.
5. Subset of ‘int’ datatype is :
a) long ,ulong, ushort
b) long, ulong, uint
c) long, float, double
d) long, float, ushort
Answer
Answer: c [Reason:] By definition.
6. 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
Answer
Answer:b [Reason:] ‘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.
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
Answer
Answer: b [Reason:] By definition.
8. For the given set of code, is conversion possible?
-
static void 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
Answer
Answer: b [Reason:] 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.
9. Predict the relevant output for the given set of code.
-
static void 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
Answer
Answer: c [Reason:] None.
Output : sum = 2.928698.
10) Which of the conversions are valid for the given set of code?
-
static void 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 = c, b = a
c) b = a, c = a
d) All of the mentioned
Answer
Answer: a [Reason:] 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 :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;
}