C# Multiple-Choice Questions (MCQs)

C# is a general-purpose, multi-paradigm programming language. C# programming language encompasses static typing, strong typing, lexically scoped, imperative, declarative, functional, generic, object-oriented, and component-oriented programming disciplines.

C# MCQs: This section contains multiple-choice questions and answers on C# programming language. It will help the students and developers to prepare well for their exams, and enhance their skills.

C# MCQs Index

  1. C# Basics MCQs
  2. C# Variables and Data Types MCQss
  3. C# Input/Output MCQs
  4. C# Conditional and Control Statements MCQs
  5. C# Strings MCQs
  6. C# Arrays MCQs
  7. C# OOP MCQs
  8. C# Miscellaneous Topics MCQs

1) C# Basics MCQs

1. C# is a programming language, developed by ___.

  1. Oracle
  2. Microsoft
  3. GNU project
  4. Google

Answer: B) Microsoft

Explanation:

C# is a programming language, developed by Microsoft.

Discuss this Question


2. C# runs on the ___.

  1. .NET Framework
  2. Java Virtual Machine
  3. Both A. and B.
  4. None of the above

Answer: A) .NET Framework

Explanation:

C# runs on the .NET Framework.

Discuss this Question


3. C# programming language is used to develop -

  1. Web apps
  2. Desktop apps
  3. Mobiles apps
  4. All of the above

Answer: D) All of the above

Explanation:

C# programming language is used to develop the web apps, desktop apps, mobile apps, games and much more.

Discuss this Question


4. Is C# an object-oriented programming language?

  1. Yes
  2. No

Answer: A) Yes

Explanation:

Yes, C# is an object-oriented programming language.

Discuss this Question


5. Is C++ an alias of C#?

  1. Yes
  2. No

Answer: B) No

Explanation:

No, C++ is not an alias of C#.

Discuss this Question


6. What is the extension of a C# language file?

  1. .c
  2. .cpp
  3. .cs
  4. .csp

Answer: C) .cs

Explanation:

The C# file is saved with ".cs" extension.

Discuss this Question


7. Who is the founder of C# programming language?

  1. Anders Hejlsberg
  2. Douglas Crockford
  3. Rasmus Lerdorf
  4. Brendan Eich

Answer: A) Anders Hejlsberg

Explanation:

C# programming language is developed by Anders Hejlsberg.

Discuss this Question


8. Is C# a type safe programming language?

  1. Yes
  2. No

Answer: A) Yes

Explanation:

Yes, C# is a type safe programming language, the C# type safe code can only access the memory location that it has permission to execute. Therefore, it improves a security of the program.

Discuss this Question


9. Is C# a structured programming language?

  1. Yes
  2. No

Answer: A) Yes

Explanation:

Yes, C# is a structured programming language that means we can break the program into parts using functions. So, it is easy to understand and modify.

Discuss this Question


10. CLR stands for ___.

  1. Common Type System
  2. Common Language Specification
  3. Common Language Runtime
  4. Java Virtual Machine

Answer: C) Common Language Runtime

Explanation:

CLR stands for Common Language Runtime.

Discuss this Question


11. .Net CLR is equivalent to?

  1. Common Type System
  2. Common Language Specification
  3. Common Language Runtime
  4. Java Virtual Machine

Answer: D) Java Virtual Machine

Explanation:

.NET CLR is equivalent to JVM (Java Virtual Machine).

Discuss this Question


12. What is CLR in C#?

  1. It is a virtual machine component of Microsoft .NET Framework
  2. It is a virtual machine component of JVM
  3. It is a compiler to compiler the C# code
  4. All of the above

Answer: A) It is a virtual machine component of Microsoft .NET Framework

Explanation:

CLR is a virtual machine component of Microsoft .NET Framework.

Discuss this Question


13. What does the CLR in C# do?

  1. Manages the execution of Webpages programs
  2. Manages the parsing of the various types
  3. Manages the execution of .NET programs
  4. All of the above

Answer: C) Manages the execution of .NET programs

Explanation:

The CLR is used to manage the execution of .NET programs.

Discuss this Question


14. SOAP in C# stands for ___.

  1. Simple Object Access Protocol
  2. Simple Object Access Program
  3. Standard Object Access Protocol
  4. Standard Object Access Program

Answer: A) Simple Object Access Protocol

Explanation:

In C# programming, the SOAP stands for Simple Object Access Protocol.

Discuss this Question


15. "Garbage collection automatically reclaims memory occupied by unreachable unused objects." – This statement is true or false in C#?

  1. True
  2. False

Answer: A) True

Explanation:

In C#, this following statement is true.

"Garbage collection automatically reclaims memory occupied by unreachable unused objects."

Discuss this Question


16. Which symbols are used to mark the beginning and end of a code block?

  1. Square brackets []
  2. Curly braces {}
  3. Round brackets ()
  4. Double-quotes ""

Answer: B) Curly braces {}

Explanation:

C# code block begins and ends by the Curly braces {}.

Discuss this Question


17. Every C# statement is terminated by ___.

  1. Colon (:)
  2. Semicolon (;)
  3. Comma (,)
  4. Dot (.)

Answer: B) Semicolon (;)

Explanation:

Every C# statement is terminated by semicolon (;).

Discuss this Question


18. Is C# programming language a case-sensitive?

  1. Yes
  2. No

Answer: A) Yes

Explanation:

Yes, C# programming language is a case-sensitive.

Discuss this Question


19. In C#, a single-line comment starts with ___.

  1. Two forward slashes (//)
  2. Two backward slashes (\\)
  3. A hash character (#)
  4. A dollar character ($)

Answer: A) Two forward slashes (//)

Explanation:

In C#, a single-line comment starts with two forward slashes (//).

Discuss this Question


20. In C#, the multi-line comments placed within the ___.

  1. // and //
  2. \\ and //
  3. <!-- and -->
  4. /* and */

Answer: D) /* and */

Explanation:

In C#, the multi-line comments placed within the /* and */.

Discuss this Question


2) C# Variables and Data Types MCQs

21. What is the correct syntax to declare a variable in C#?

  1. type variableName = value;
  2. type variableName;
  3. variableName as type = value;
  4. Both A. and B.

Answer: D) Both A. and B.

Explanation:

The both of the above syntaxes can be used to declare a variable:

  • type variableName = value; - It defines the type and assigns the value
  • type variableName; - It defines the type only

Discuss this Question


22. Which data type is used to store text value in C#?

  1. text
  2. txt
  3. string
  4. str

Answer: C) string

Explanation:

The string data type is used to store text value in C#.

Discuss this Question


23. Which C# keyword is used to define a constant?

  1. define
  2. fixed
  3. constant
  4. const

Answer: D) const

Explanation:

The const is used to define a constant in C#.

Discuss this Question


24. What is the correct syntax to define a C# constant?

  1. const type constant_name;
  2. const type constant_name = value;
  3. const constant_name as type;
  4. const constant_name as type = value;

Answer: B) const type constant_name = value;

Explanation:

The correct syntax to define a C# constant is:

const type constant_name = value;

Note: A const field requires a value to be provided.

Discuss this Question


25. Which is not a valid C# data type?

  1. long
  2. int
  3. float
  4. complex

Answer: D) complex

Explanation:

There is no complex type in C#.

Discuss this Question


26. Which is the correct order for implicit type conversion to convert a smaller to a larger type in C#?

  1. char -> int -> long -> float -> double
  2. bool -> char -> int -> long -> float -> double
  3. char -> int -> float -> long -> double
  4. bool -> char -> int -> long -> double -> float

Answer: A) char -> int -> long -> float -> double

Explanation:

The implicit type conversion is done in the following order: char -> int -> long -> float -> double.

Discuss this Question


27. Which is the correct order for explicit type conversion to convert a larger to a smaller type in C#?

  1. double -> float -> long -> int -> char -> bool
  2. double -> float -> long -> int -> char
  3. float -> double -> long -> int -> char
  4. float -> double -> long -> int -> char -> bool

Answer: B) double -> float -> long -> int -> char

Explanation:

The explicit type conversion is done in the following order: double -> float -> long -> int -> char.

Discuss this Question


28. Which is the correct C# statement to convert a float value to int explicitly?

  1. int_variable = (int) float_variable;
  2. int_variable = float_variable;
  3. int_variable = (int) (float_variable);
  4. int_variable = int *float_variable);

Answer: A) int_variable = (int) float_variable;

Explanation:

The following syntax is used to convert a float value to int explicitly:

int_variable = (int) float_variable;

Discuss this Question


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

using System;

namespace MyApplication {
  class Program {
    static void Main(string[] args) {
      bool x = true;
      Console.Write(Convert.ToString(x));
    }
  }
}
  1. True
  2. true
  3. False
  4. false

Answer: A) True

Explanation:

In the above C# code, we are using Convert.ToString() method which converts bool to string. Thus, the output will be "True".

Discuss this Question


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

using System;

namespace MyApplication {
  class Program {
    static void Main(string[] args) {
      double x = 10.25;
      Console.Write(Convert.ToInt32(x));
    }
  }
}
  1. 10.30
  2. 10.25
  3. 10
  4. Error

Answer: C) 10

Explanation:

In the above C# code, we are using Convert.ToInt32() method which converts double to int. Thus, the output will be "10".

Discuss this Question


3) C# Input/Output MCQs

31. What is 'Console' in C#?

  1. Class
  2. Object
  3. Method
  4. Structure

Answer: A) Class

Explanation:

In C#, the Console class is used to represent the standard input, output, and error streams for the console applications.

Discuss this Question


32. What will be the output of the following C# code, if the input is 123?

using System;

namespace MyApplication {
  class Program {
    static void Main(string[] args) {
      Console.WriteLine("Enter a number:");
      int num = Console.ReadLine();
      Console.WriteLine("Given number is: " + num);
    }
  }
}
  1. Given number is:123
  2. Given number is: 123
  3. Given number is: "123"
  4. Error

Answer: D) Error

Explanation:

In C#, Console.ReadLine() is used to read the string and here we are trying to input an integer value. Thus, the output will be an error.

Cannot implicitly convert type `string' to `int'

Discuss this Question


33. What will be the output of the following C# code, if the input is 123?

using System;

namespace MyApplication {
  class Program {
    static void Main(string[] args) {
      Console.WriteLine("Enter a number:");
      int num = Convert.ToInt32(Console.ReadLine());
      Console.WriteLine("Given number is: " + num);
    }
  }
}
  1. Given number is:123
  2. Given number is: 123
  3. Given number is: "123"
  4. Error

Answer: B) Given number is: 123

Explanation:

In C#, Console.ReadLine() is used to read the string and here the input is 123 and converting it into an integer. This, there will not be an error.

Output will be: Given number is: 123

Discuss this Question


34. Which is/are the correct method(s) to input a float value in C#?

  1. Parse(Console.ReadLine())
  2. ToSingle(Console.ReadLine())
  3. ToFloat(Console.ReadLine());
  4. Both A and B
  5. Both A and C

Answer: B) Both A and B

Explanation:

In C#, the following methods can be used to input a float value:

float.Parse(Console.ReadLine());
Convert.ToSingle(Console.ReadLine());

Discuss this Question


35. What is the difference between Console.Write() and Console.WriteLine() methods in C#?

  1. Write() writes a single character while Console.WriteLine() writes a complete line
  2. Write() writes the data on the console without printing a newline while Console.WriteLine() writes the data on the console along with printing a newline
  3. ToFloat(Console.ReadLine());
  4. Write() writes the string on the console while Console.WriteLine() writes the string as well as values of the variables on the console
  5. Both can be used for the same purpose

Answer: B) Write() writes the data on the console without printing a newline while Console.WriteLine() writes the data on the console along with printing a newline

Explanation:

The only difference between the Console.Write() and Console.WriteLine() is that the Console.Write() is used to write data without printing the new line, while Console.WriteLine() is used to write data along with printing the new line.

Discuss this Question


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

using System;

namespace MyApplication {
  class Program {
    static void Main(string[] args) {
      int a = 10, b = 20;
      Console.WriteLine("{0},{0}", a, b);
    }
  }
}
  1. 10,10
  2. 10,20
  3. 20,20
  4. Error

Answer: A) 10,10

Explanation:

In the above code, there are two variables a and b, but while printing the values of a and b, we are using the same placeholder {0}, that will print the value of the first variable. Thus, the output is 10,10.

Discuss this Question


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

using System;

namespace MyApplication {
  class Program {
    static void Main(string[] args) {
      int a = 10, b = 20;
      Console.WriteLine("{0}+{1}", a, b);
    }
  }
}
  1. 20
  2. 30
  3. 10+20
  4. 10+10

Answer: C) 10+20

Explanation:

In the statement, Console.WriteLine("{0}+{1}", a, b); - {0} is the placeholder for variable a and {1} is the placeholder for variable b, {0}+{1} will not perform any operation, values of a and b will be printed at the place of {0} and {1}. Thus, the output will be 10+20.

Discuss this Question


4) C# Conditional and Control Statements MCQs

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

using System;

class Program {
  static void Main(string[] args) {
    int i = 2;
    int j = 10 / 4;

    if (i == j) {
      Console.WriteLine("True");
    } else {
      Console.WriteLine("False");
    }
  }
}
  1. True
  2. False
  3. Error
  4. None

Answer: A) True

Explanation:

The statement int j = 10/4; The value of j will be 2 because of implicit type casting. Thus, the output will be "True".

Discuss this Question


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

using System;

class Program {
  static void Main(string[] args) {
    Console.WriteLine(true ^ true);
  }
}
  1. True
  2. False
  3. Error
  4. None

Answer: B) False

Explanation:

Here, we are using the Logical exclusive OR operator ^ (XOR). It returns true when one operand evaluates to true and another operand evaluates to false. In the above code, both of the operands are true. Thus, the output will be False.

Discuss this Question


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

using System;

class Program {
  static void Main(string[] args) {
    Console.WriteLine(true && false);
  }
}
  1. True
  2. False
  3. Error
  4. None

Answer: B) False

Explanation:

Here, we are using the Conditional logical AND operator (&&). It returns true of both of the operands evaluate to true. In the above code, the operands are true, false. Thus, the output will be "False".

Discuss this Question


41. What is the correct syntax for do while loop in C#?

  1. do; {
     statement(s);
    } while (test_condition);
    
  2. do
    {
     statement(s);
    } while (test_condition)
    
  3. do(test_condition) {
    statement(s);
    }while;
    
  4. do {
     statement(s);
    } while (test_condition);
    

Answer: D)

do {
 statement(s);
} while (test_condition);

Explanation:

The correct syntax of do...while statement is:

do {
 statement(s);
} while (test_condition);

Discuss this Question


42. Does C# support foreach loop?

  1. Yes
  2. No

Answer: A) Yes

Explanation:

Yes, C# supports foreach loop. The foreach loop is used to iterate over the elements of the collection. The collection may be an array or a list.

Example:

using System;
class Program {
  static void Main(string[] args) {
    // an array
    int[] arr = new int[] {10, 20, 30, 40, 50};

    foreach(int elements in arr) {
      Console.WriteLine(elements);
    }
  }
}
/*
Output:
10
20
30
40
50
*/

Discuss this Question


43. What is the correct syntax for foreach statement in C#?

  1. foreach(type variable_name in collection_name); {
      statement(s);
    }
    
  2. foreach(type variable_name in collection_name) {
      statement(s);
    }
    
  3. foreach(variable_name As Type in collection_name){
      statement(s);
    }
    
  4. foreach(dim type variable_name in collection_name) {
      statement(s);
    }
    

Answer: B)

foreach(type variable_name in collection_name) {
  statement(s);
}

Explanation:

The correct syntax of foreach statement is:

foreach(type variable_name in collection_name) {
  statement(s);
}

Discuss this Question


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

using System;

class Program {
  static void Main(string[] args) {
    int i = 100;

    do {
      Console.Write(i + " ");
      ++i;
    } while (i <= 50);
  }
}
  1. Error
  2. 100 101 102 ... Infinite
  3. 101
  4. 100

Answer: D) 100

Explanation:

The do-while loop is an exit control loop where the condition is checked after executing the loop body. In the above code, the condition is false still loop body will be executed first. Thus, the output will be 100.

Discuss this Question


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

using System;

class Program {
  public static void Main() {
    int i = 10;
    Console.WriteLine(i++);
  }
}
  1. 10
  2. 11
  3. 12
  4. Error

Answer: A) 10

Explanation:

The statement i++ is a post-increment operation, and it increases the value of i after evaluating the Console.WriteLine() statement. Thus, 10 will be printed.

Discuss this Question


46. Which is the correct for() statement to run an infinite loop?

  1. for(;;)
  2. for(;;);
  3. for(;;)
  4. for(1;1;1)

Answer: A) for(;;)

Explanation:

The correct for statement/loop to run an infinite loop is:

for(;;)

Discuss this Question


47. Can we obtain the array index using foreach loop in C#?

  1. Yes
  2. No

Answer: B) No

Explanation:

No, we cannot obtain the array index using foreach loop because it does not keep track of the index, it only iterates forward over the array in single steps.

Discuss this Question


48. Does C# support goto statement?

  1. Yes
  2. No

Answer: A) Yes

Explanation:

Yes, C# supports the goto statement. The goto statement transfers the control of the program to the specified label and the label is the valid identifier and placed just before the statement from where the control is transferred.

Example:

using System;

class Program {
  public static void Main() {
    Console.WriteLine("In the main...");
    int i = 1;

    okay:
      Console.Write(i + " ");
    if (++i <= 10) {
      goto okay;
    }

  }
}

/*
Output:
In the main...
1 2 3 4 5 6 7 8 9 10 
*/

Discuss this Question


49. What is the use of 'throw' statement in C#?

  1. to return from the calling functions to called function
  2. to throw an exception manually during the execution of the program
  3. to return from the switch statement
  4. None of the above

Answer: B) to throw an exception manually during the execution of the program

Explanation:

The C# throw statement is used to throw an exception manually during the execution of the program.

Discuss this Question


50. Which C# keyword is used to coming out from the loop?

  1. break
  2. continue
  3. Both A and B
  4. None of the above

Answer: A) break

Explanation:

The C# break keyword breaks the execution of the loop and sends the program’s control to the next statement written after the loop.

Discuss this Question


5) C# Strings MCQs

51. What is String in C# meant for?

  1. A variable
  2. A Class
  3. An Array
  4. An object

Answer: D) An object

Explanation:

In C#, String is an object of System.String class.

Discuss this Question


52. Can we use == operator to compare two strings?

  1. Yes
  2. No

Answer: A) Yes

Explanation:

Two strings can be compared by using the == Operator.

Discuss this Question


53. Which is the base class of the String() Constructor?

  1. String
  2. System.IO.String
  3. System.Strings
  4. System.String

Answer: D) System.String

Explanation:

In C#, System.String is a base class for all String related methods, properties, constructors, and operators.

Discuss this Question


54. Which String class operator is used to determine whether two specified strings have different values?

  1. !=
  2. !
  3. ~
  4. ~

Answer: A) !=

Explanation:

In C#, the inequality operator (!=) can be used to determine whether two specified strings have different values.

Syntax of inequality operators is:

public static bool operator != (string? a, string? b);

Discuss this Question


55. Except == operator, which methods can be used to compare two strings?

  1. Equals()
  2. Compare()
  3. Both A and B
  4. None of these

Answer: C) Both A and B

Explanation:

The Equals() and Compare() methods can also be used to compare two strings in C#.

Discuss this Question


56. Which is the correct method to convert given string in uppercase?

  1. Upper()
  2. ToUpper()
  3. Upr()
  4. ToUpr()

Answer: B) ToUpper()

Explanation:

The ToUpper() method is used to convert the given string into the uppercase.

Example:

using System;

class Program {
  public static void Main() {
    string str = "includehelp";
    Console.WriteLine(str.ToUpper());
  }
}

Discuss this Question


57. What is String.Length in C#?

  1. Property
  2. Method
  3. Constructor
  4. Both A and B

Answer: A) Property

Explanation:

The String.Length is property of System.String class that is used to get the number of characters in the current String object.

Syntax of String.Length property is:

public int Length { get; }

Discuss this Question


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

using System;

class Program {
  static void Main(string[] args) {
    String s1 = "Hello";
    String s2 = "IncludeHelp";
    String s3 = s1;
    Console.WriteLine(s1.Equals(s3) + "  " + s2.CompareTo(s1));
  }
}
  1. Error
  2. True True
  3. True False
  4. True 1

Answer: D) True 1

Explanation:

The Equals() method returns a bool value. We assigned s1 to s3. Thus, the statement s1.Equals(s3) will returns "True". And, the CompareTo() method returns an int value (Less than 0, o, or greater than 0). The statement s2.CompareTo(s1) will returns 1 because this instance (s2) follows s1.

Discuss this Question


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

using System;

class Program {
  static void Main(string[] args) {
    String str = "Hello";
    Console.WriteLine(str.Length);

  }
}
  1. 5
  2. 6
  3. 7
  4. 4

Answer: A) 5

Explanation:

The String.Length returns the length of the string. Thus, the output will be 5.

Discuss this Question


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

using System;

class Program {
  static void Main(string[] args) {
    String str = "Hello";
    Console.WriteLine(str.IndexOf('h'));
  }
}
  1. 0
  2. 1
  3. -1
  4. Error

Answer: C) -1

Explanation:

The IndexOf() method returns the index of a given characters, and it returns -1 if the given character is not found in the string. Here, we are trying to get the index of 'h' which is not present in the string. Thus, the output will be -1.

Discuss this Question


6) C# Arrays MCQs

61. To declare an array, define the variable type with ___.

  1. Square brackets []
  2. Curly brackets {}
  3. Round brackets ()
  4. None of the above

Answer: A) Square brackets []

Explanation:

To declare an array, define the variable type with square brackets [].

Discuss this Question


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

using System;

namespace MyApplication {
  class Program {
    static void Main(string[] args) {
      string[] mobiles = { "iPhone", "Samsung", "Vivo"};
      Console.WriteLine(mobiles);
    }
  }
}
  1. "iPhone", "Samsung", "Vivo"
  2. {"iPhone", "Samsung", "Vivo"}
  3. string[]
  4. System.String[]

Answer: D) System.String[]

Explanation:

In the above code, the statement Console.WriteLine(mobiles); will print the type of the variable that is System.String[].

Discuss this Question


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

using System;

namespace MyApplication {
  class Program {
    static void Main(string[] args) {
      string[] mobiles = {"iPhone", "Samsung", "Vivo"};
      Console.WriteLine(mobiles[-1]);
    }
  }
}
  1. None
  2. Warning
  3. Exception
  4. System.String[]

Answer: C) Exception

Explanation:

The array index starts from 0 in C#, in the above code – we are trying to get the element with index -1. Thus, there will be an exception "System.IndexOutOfRangeException: Index was outside the bounds of the array."

Discuss this Question


64. Which statement is correct about the following C# statement?

int[] x= {10, 20, 30};
  1. 'x' is a reference to the array created on stack
  2. 'x' is a reference to an object created on stack
  3. 'x' is a reference to an object of a 'System.Array' class
  4. None of the above

Answer: C) 'x' is a reference to an object of a 'System.Array' class

Explanation:

In the above given C# statement, the variable 'x' is a reference to an object of a 'System.Array' class.

Discuss this Question


65. Which array property is used to get the total number of elements in C#?

  1. Len
  2. Length
  3. Elements
  4. MaxLen

Answer: B) Length

Explanation:

The Length property is used to get the total number of elements in C#.

Discuss this Question


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

using System;

namespace MyApplication {
  class Program {
    static void Main(string[] args) {
      string[] mobiles = {"iPhone", "Samsung", "Vivo"};
      Console.WriteLine(mobiles[0] + mobiles[2]);
    }
  }
}
  1. iPhoneVivo
  2. iPhone+Vivo
  3. Exception
  4. iPhone Vivo

Answer: A) iPhoneVivo

Explanation:

In the above code, the statement mobiles[0]+mobiles[2] will concatenate the 0th and 2nd elements that are "iPhone" and "Vivo". Thus, the output will be "iPhoneVivo".

Discuss this Question


67. Which array method is used to sort an array alphabetically or in an ascending order in C#?

  1. sort()
  2. sorting()
  3. Sort()
  4. Sorting()

Answer: C) Sort()

Explanation:

The Sort() method is used to sort an array alphabetically or in an ascending order in C#.

Discuss this Question


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

using System;

namespace MyApplication {
  class Program {
    static void Main(string[] args) {
      string[] mobiles = {"iPhone", "Samsung", "Vivo"};
      Console.WriteLine(mobiles[0] + mobiles[2]);
    }
  }
}
  1. iPhone
  2. Vivo
  3. iPhoneVivo
  4. Runtime Error

Answer: C) iPhoneVivo

Discuss this Question


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

using System;

namespace MyApplication {
  class Program {
    static void Main(string[] args) {
      int[,] ARR = {{1,2},{3,4}};
      Console.WriteLine(ARR.GetLength(0)+","+ARR.GetLength(1));
    }
  }
}
  1. 4,4
  2. 2,2
  3. Compilation Error
  4. Runtime Error

Answer: B) 2,2

Explanation:

The above code will print the length of the of the first and second rows of the above two-dimensional array.

Discuss this Question


70. Which of the correct syntax to declare an array with 2 rows and 3 columns in C#?

  1. int arr[2][3] = new int[2][3];
  2. int arr[2,3] = new int[2,3];
  3. int[,] arr = new int[2,3];
  4. int [,]arr = new [2,3]int;

Answer: C) int[,] arr = new int[2,3];

Explanation:

The correct way to declare an array with 2 rows and 3 columns in C# is:

int[,] arr = new int[2,3];

Discuss this Question


7) C# OOP MCQs

71. Which keyword is used to define a class in C#?

  1. Class
  2. class
  3. System.Class
  4. OOPS.class

Answer: B) class

Explanation:

The class keyword is used to define a class in C#.

Syntax:

class class_name
{
 // Class Definition
}

Discuss this Question


72. Which is the correct way to declare an object of the class in C#?

  1. Class_Name Object_Name = new Class_Name();
  2. Class_Name Object_Name;
  3. new Object_Name as Class_Name();
  4. Both A and B

Answer: A) Class_Name Object_Name = new Class_Name();

Explanation:

The correct way to declare an object of the class in C# is:

Class_Name Object_Name = new Class_Name();

Consider the below example:

using System;

namespace MyApplication {
  class Mobiles {
    string brand = "Apple";

    static void Main(string[] args) {
      Mobiles mobile = new Mobiles();
      Console.WriteLine(mobile.brand);
    }
  }
}

// Output: Apple

Discuss this Question


73. Which operator is used to access variables/fields inside a class in C#?

  1. Arrow Operator (->)
  2. Dot Operator (.)
  3. Greater Than (>)
  4. Dot and Greater Than (.>)

Answer: B) Dot Operator (.)

Explanation:

The dot (.) symbol (or dot operator) is used to access variables/fields in a class in C#.

Syntax:

Class_Name.Variable_Name/Field_Name;

Discuss this Question


74. Is overriding of a function possible in the same class in C#?

  1. Yes
  2. No

Answer: B) No

Explanation:

No, overriding of a function is not possible in the same class in C#. As per the explanation on MSDN – The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.

Discuss this Question


75. Which is not a type of constructor in C#?

  1. Static Constructor
  2. Private Constructor
  3. Body Constructor
  4. Parameterized Constructor

Answer: C) Body Constructor

Explanation:

There are basically 5 types of the constructors in C#, they are:

  • Static constructor
  • Private constructor
  • Copy constructor
  • Default constructor
  • Parameterized constructor

Discuss this Question


76. How many types of access modifiers in C#?

  1. 2
  2. 3
  3. 4
  4. 5

Answer: C) 4

Explanation:

In C#, there are 4 types of access modifiers, they are:

  • public
  • private
  • protected
  • internal

Discuss this Question


77. What does the access modifier do in C#?

  1. To maintain the syntax
  2. To define a variable inside the class
  3. To access the variables defined inside the class
  4. To control the visibility of class members

Answer: D) To control the visibility of class members

Explanation:

In C#, there are 4 types of access modifiers they are public, private, protected, and internal. And these access modifiers are used to control the visibility of class members (the security level of each individual class and class member).

Discuss this Question


78. The internal access modifier is used for ___.

  1. Types and type members
  2. Defining a field that can be accessed in all classes
  3. Defining a field that can be accessed in inherited classes
  4. All of the above

Answer: A) Types and type members

Explanation:

In C#, the internal access modifier is used for types and type members. The internal types or members (those are defined by the using of internal keyword/ access modifier) are accessible only within files in the same assembly.

Discuss this Question


79. The protected access modifier defines a member that can be accessible within ___.

  1. its class and all other classes
  2. its class and by derived class instances
  3. its class only
  4. None of the above

Answer: B) its class and by derived class instances

Explanation:

In C#, the protected access modifier defines a member that can be accessible within its class and by derived class instances.

Discuss this Question


80. Does C# support multiple inheritance?

  1. Yes
  2. No

Answer: B) No

Explanation:

No, C# does not support multiple class inheritance.

Discuss this Question


81. Which C# concept has the capability of an object to take number of different forms and hence display behaviour as accordingly?

  1. Polymorphism
  2. Encapsulation
  3. Abstraction
  4. None of the above

Answer: A) Polymorphism

Explanation:

In C#, the concept has the capability of an object to take number of different forms and hence display behaviour as accordingly known as "Polymorphism".

Discuss this Question


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

using System;

namespace MyApplication {
  public class Class1 {
    public static int x = 10;

  }
  public class Class2: Class1 {
    public static int x = 20;
    static void Main(string[] args) {
      Console.WriteLine(x + ", " + Class1.x);
    }
  }
}
  1. 20, 20
  2. 10, 10
  3. 20, 10
  4. Exception

Answer: C) 20, 10

Explanation:

In the above code, we are printing the values of x which is the member of Class2 and Class1.x which is the member of Class1. Thus, the output will be "20, 10".

Discuss this Question


83. What is the C# keyword which is used to change data and behaviour of a base class by replacing a member of the base class with a new derived member?

  1. overrides
  2. protected
  3. base
  4. new

Answer: D) new

Explanation:

The C# keyword new is used to change data and behaviour of a base class by replacing a member of the base class with a new derived member.

Discuss this Question


84. Which keyword is used to declare an interface in C#?

  1. interface
  2. Interface
  3. implement
  4. Implement

Answer: A) interface

Explanation:

In C#, an interface can be declared using the interface keyword. An interface can contain declarations of methods, properties, indexers, and events.

Discuss this Question


85. In C#, by default, the default interface methods are ___.

  1. virtual
  2. sealed
  3. private
  4. public

Answer: A) virtual

Explanation:

In C#, by default, the default interface methods are virtual unless the sealed or private modifier is used.

Discuss this Question


86. In C#, the objects created using new operator are stored in ___.

  1. Cache Memory
  2. Stack Memory
  3. Heap Memory
  4. None of the above

Answer: C) Heap Memory

Explanation:

In C#, the objects created using new operator are stored in Heap Memory.

Discuss this Question


87. Which of the following cannot be used to declare an interface in C#?

  1. Methods
  2. Properties
  3. Events
  4. Structures

Answer: D) Structures

Explanation:

In C#, to declare an interface – we cannot use structures.

Discuss this Question


88. Which of the access specifier is used in interface in C#?

  1. private
  2. public
  3. protected
  4. All of the above

Answer: B) public

Explanation:

In C#, the interface members are always public, because the purpose of an interface is to enable other types to access a class or struct. No other access modifiers can be applied to interface members.

Discuss this Question


89. Which type of class does not have its own objects but acts as a base class for its subclass in C#?

  1. Abstract Class
  2. Static Class
  3. Sealed Class
  4. Protected Class

Answer: A) Abstract Class

Explanation:

In C#, the Abstract Class does not have its own objects but acts as a base class for its subclass.

Discuss this Question


90. Which modifier is used to while redefining an abstract method by a derived class in C#?

  1. Overloads
  2. New
  3. Overrides
  4. Virtual

Answer: C) Overrides

Explanation:

In C#, the Overrides modifier is used to while redefining an abstract method by a derived class.

Discuss this Question


8) C# Miscellaneous Topics MCQs

91. Does C# support pointers?

  1. Yes
  2. No

Answer: A) Yes

Explanation:

Yes, C# supports pointers in a limited extent.

Discuss this Question


92. A C# pointer is used to store the ___ of another type.

  1. Value
  2. Memory address
  3. Size of the type
  4. Reference of the variable

Answer: B) Memory address

Explanation:

A C# pointer is used to store the memory address of another type.

Discuss this Question


93. C# pointer can only be declared to hold the memory address of ___.

  1. value types and arrays
  2. reference types
  3. pointer types
  4. reference and pointer types

Answer: A) value types and arrays

Explanation:

C# pointer can only be declared to hold the memory address of value types and arrays. Unlike reference types, pointer types are not tracked by the default garbage collection mechanism.

Discuss this Question


94. How many catch blocks can be used with a single try block in C#?

  1. One
  2. Two
  3. Many
  4. None of the above

Answer: C) Many

Explanation:

In C#, there can be multiple (many) catch blocks with a single try block.

Discuss this Question


95. Is the use of return statement necessary in every function in C#?

  1. Yes
  2. No

Answer: A) Yes

Explanation:

Yes, the use of return statement is necessary in every function in C#.

Discuss this Question


96. Which access specifier should be used for Main() method in C#?

  1. private
  2. public
  3. protected
  4. internal

Answer: B) public

Explanation:

The C# Main() method should be defined as public because it is called by runtime. By default Main() method is private.

Discuss this Question


97. In C#, a namespace is the collection of classes?

  1. True
  2. False

Answer: A) True

Explanation:

Yes, a namespace is the collection of classes.

Discuss this Question


98. Which is the C# class from which data type UInt is derived?

  1. System.Int16
  2. System.Int32
  3. System.UInt16
  4. System.UInt32

Answer: D) System.UInt32

Explanation:

The Uint data type is derived from System.UInt32 class.

Discuss this Question


99. Which is the first line of a C# program?

  1. using System;
  2. using system;
  3. using Namespace;
  4. namespace MyApplication

Answer: A) using System;

Explanation:

The first line of a C# program is:

using System;

Discuss this Question


100. In C#, structures are used for ___ objects such as mobiles, cars, etc.

  1. Lightweight
  2. Darkweight
  3. Extra
  4. Compress

Answer: A) Lightweight

Explanation:

In C#, structures are used for lightweight objects such as mobiles, cars, etc.

Discuss this Question





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.