Home »
MCQs
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
- C# Basics MCQs
- C# Variables and Data Types MCQss
- C# Input/Output MCQs
- C# Conditional and Control Statements MCQs
- C# Strings MCQs
- C# Arrays MCQs
- C# OOP MCQs
- C# Miscellaneous Topics MCQs
1) C# Basics MCQs
1. C# is a programming language, developed by ___.
- Oracle
- Microsoft
- GNU project
- Google
Answer: B) Microsoft
Explanation:
C# is a programming language, developed by Microsoft.
Discuss this Question
2. C# runs on the ___.
- .NET Framework
- Java Virtual Machine
- Both A. and B.
- 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 -
- Web apps
- Desktop apps
- Mobiles apps
- 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?
- Yes
- No
Answer: A) Yes
Explanation:
Yes, C# is an object-oriented programming language.
Discuss this Question
5. Is C++ an alias of C#?
- Yes
- 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?
- .c
- .cpp
- .cs
- .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?
- Anders Hejlsberg
- Douglas Crockford
- Rasmus Lerdorf
- 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?
- Yes
- 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?
- Yes
- 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 ___.
- Common Type System
- Common Language Specification
- Common Language Runtime
- Java Virtual Machine
Answer: C) Common Language Runtime
Explanation:
CLR stands for Common Language Runtime.
Discuss this Question
11. .Net CLR is equivalent to?
- Common Type System
- Common Language Specification
- Common Language Runtime
- 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#?
- It is a virtual machine component of Microsoft .NET Framework
- It is a virtual machine component of JVM
- It is a compiler to compiler the C# code
- 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?
- Manages the execution of Webpages programs
- Manages the parsing of the various types
- Manages the execution of .NET programs
- 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 ___.
- Simple Object Access Protocol
- Simple Object Access Program
- Standard Object Access Protocol
- 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#?
- True
- 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?
- Square brackets []
- Curly braces {}
- Round brackets ()
- 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 ___.
- Colon (:)
- Semicolon (;)
- Comma (,)
- Dot (.)
Answer: B) Semicolon (;)
Explanation:
Every C# statement is terminated by semicolon (;).
Discuss this Question
18. Is C# programming language a case-sensitive?
- Yes
- 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 ___.
- Two forward slashes (//)
- Two backward slashes (\\)
- A hash character (#)
- 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 ___.
- // and //
- \\ and //
- <!-- and -->
- /* 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#?
- type variableName = value;
- type variableName;
- variableName as type = value;
- 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#?
- text
- txt
- string
- 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?
- define
- fixed
- constant
- 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?
- const type constant_name;
- const type constant_name = value;
- const constant_name as type;
- 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?
- long
- int
- float
- 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#?
- char -> int -> long -> float -> double
- bool -> char -> int -> long -> float -> double
- char -> int -> float -> long -> double
- 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#?
- double -> float -> long -> int -> char -> bool
- double -> float -> long -> int -> char
- float -> double -> long -> int -> char
- 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?
- int_variable = (int) float_variable;
- int_variable = float_variable;
- int_variable = (int) (float_variable);
- 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));
}
}
}
- True
- true
- False
- 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));
}
}
}
- 10.30
- 10.25
- 10
- 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#?
- Class
- Object
- Method
- 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);
}
}
}
- Given number is:123
- Given number is: 123
- Given number is: "123"
- 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);
}
}
}
- Given number is:123
- Given number is: 123
- Given number is: "123"
- 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#?
- Parse(Console.ReadLine())
- ToSingle(Console.ReadLine())
- ToFloat(Console.ReadLine());
- Both A and B
- 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#?
- Write() writes a single character while Console.WriteLine() writes a complete line
- 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
- ToFloat(Console.ReadLine());
- Write() writes the string on the console while Console.WriteLine() writes the string as well as values of the variables on the console
- 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);
}
}
}
- 10,10
- 10,20
- 20,20
- 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);
}
}
}
- 20
- 30
- 10+20
- 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");
}
}
}
- True
- False
- Error
- 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);
}
}
- True
- False
- Error
- 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);
}
}
- True
- False
- Error
- 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#?
-
do; {
statement(s);
} while (test_condition);
-
do
{
statement(s);
} while (test_condition)
-
do(test_condition) {
statement(s);
}while;
-
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?
- Yes
- 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#?
-
foreach(type variable_name in collection_name); {
statement(s);
}
-
foreach(type variable_name in collection_name) {
statement(s);
}
-
foreach(variable_name As Type in collection_name){
statement(s);
}
-
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);
}
}
- Error
- 100 101 102 ... Infinite
- 101
- 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++);
}
}
- 10
- 11
- 12
- 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?
- for(;;)
- for(;;);
- for(;;)
- 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#?
- Yes
- 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?
- Yes
- 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#?
- to return from the calling functions to called function
- to throw an exception manually during the execution of the program
- to return from the switch statement
- 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?
- break
- continue
- Both A and B
- 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?
- A variable
- A Class
- An Array
- 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?
- Yes
- 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?
- String
- System.IO.String
- System.Strings
- 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?
- !=
- !
- ~
- ~
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?
- Equals()
- Compare()
- Both A and B
- 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?
- Upper()
- ToUpper()
- Upr()
- 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#?
- Property
- Method
- Constructor
- 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));
}
}
- Error
- True True
- True False
- 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);
}
}
- 5
- 6
- 7
- 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'));
}
}
- 0
- 1
- -1
- 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 ___.
- Square brackets []
- Curly brackets {}
- Round brackets ()
- 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);
}
}
}
- "iPhone", "Samsung", "Vivo"
- {"iPhone", "Samsung", "Vivo"}
- string[]
- 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]);
}
}
}
- None
- Warning
- Exception
- 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};
- 'x' is a reference to the array created on stack
- 'x' is a reference to an object created on stack
- 'x' is a reference to an object of a 'System.Array' class
- 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#?
- Len
- Length
- Elements
- 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]);
}
}
}
- iPhoneVivo
- iPhone+Vivo
- Exception
- 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#?
- sort()
- sorting()
- Sort()
- 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]);
}
}
}
- iPhone
- Vivo
- iPhoneVivo
- 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));
}
}
}
- 4,4
- 2,2
- Compilation Error
- 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#?
- int arr[2][3] = new int[2][3];
- int arr[2,3] = new int[2,3];
- int[,] arr = new int[2,3];
- 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#?
- Class
- class
- System.Class
- 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#?
- Class_Name Object_Name = new Class_Name();
- Class_Name Object_Name;
- new Object_Name as Class_Name();
- 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#?
- Arrow Operator (->)
- Dot Operator (.)
- Greater Than (>)
- 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#?
- Yes
- 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#?
- Static Constructor
- Private Constructor
- Body Constructor
- 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#?
- 2
- 3
- 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#?
- To maintain the syntax
- To define a variable inside the class
- To access the variables defined inside the class
- 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 ___.
- Types and type members
- Defining a field that can be accessed in all classes
- Defining a field that can be accessed in inherited classes
- 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 ___.
- its class and all other classes
- its class and by derived class instances
- its class only
- 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?
- Yes
- 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?
- Polymorphism
- Encapsulation
- Abstraction
- 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);
}
}
}
- 20, 20
- 10, 10
- 20, 10
- 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?
- overrides
- protected
- base
- 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#?
- interface
- Interface
- implement
- 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 ___.
- virtual
- sealed
- private
- 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 ___.
- Cache Memory
- Stack Memory
- Heap Memory
- 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#?
- Methods
- Properties
- Events
- 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#?
- private
- public
- protected
- 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#?
- Abstract Class
- Static Class
- Sealed Class
- 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#?
- Overloads
- New
- Overrides
- 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?
- Yes
- 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.
- Value
- Memory address
- Size of the type
- 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 ___.
- value types and arrays
- reference types
- pointer types
- 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#?
- One
- Two
- Many
- 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#?
- Yes
- 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#?
- private
- public
- protected
- 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?
- True
- 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?
- System.Int16
- System.Int32
- System.UInt16
- 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?
- using System;
- using system;
- using Namespace;
- 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.
- Lightweight
- Darkweight
- Extra
- Compress
Answer: A) Lightweight
Explanation:
In C#, structures are used for lightweight objects such as mobiles, cars, etc.
Discuss this Question