0% found this document useful (0 votes)
2 views72 pages

C# Question

The document contains a series of multiple-choice questions and answers related to C# programming, covering topics such as file handling, language features, data types, and input/output operations. It provides correct answers and explanations for each question, helping to reinforce understanding of C# concepts. The questions range from basic syntax to more advanced topics like exception handling and type conversion.

Uploaded by

nuredinaman424
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views72 pages

C# Question

The document contains a series of multiple-choice questions and answers related to C# programming, covering topics such as file handling, language features, data types, and input/output operations. It provides correct answers and explanations for each question, helping to reinforce understanding of C# concepts. The questions range from basic syntax to more advanced topics like exception handling and type conversion.

Uploaded by

nuredinaman424
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1 Which class in C# is used to represent a file?

a) FileStream b) File c) StreamWriter d) StreamReader

2 Which method is used to create a new file?

a) CreateFile() b) [Link]() c) NewFile() d) OpenFile()

3 Which method is used to open an existing file for reading?

a) OpenFile() b) [Link]() c) ReadFile() d) OpenText()

4 Which method is used to open an existing file for writing?

a) OpenFile() b) [Link]() c) WriteFile() d) OpenText()

5 Which method is used to read text from a file?

a) ReadLine() b) Read() c) ReadAllText() d) ReadAllLines()

6 Which method is used to write text to a file?

a) WriteLine() b) Write() c) WriteAllText() d) WriteAllLines()

7 Which method is used to close a file?

a) Close() b) Flush() c) Dispose() d) All of the above

8 What is the difference between a FileStream and a StreamReader?

a) A FileStream is used to read and write bytes, while a StreamReader is used to read text. b) A
FileStream is used to read and write characters, while a StreamReader is used to read and write lines of
text. c) A FileStream is used for low-level file access, while a StreamReader is used for high-level file
access. d) A FileStream is used for binary data, while a StreamReader is used for text data.

9 What is the difference between the [Link]() and [Link]() methods? a) The
[Link]() method reads the entire contents of a file into a single string, while the
[Link]() method reads the entire contents of a file into an array of strings. b) The
[Link]() method is used for text files, while the [Link]() method is used for binary
files. c) The [Link]() method is faster than the [Link]() method. d) The
[Link]() method is more memory-efficient than the [Link]() method.

10 How do you handle exceptions when performing file I/O?

a) Use a try-catch block. b) Use a finally block. c) Use a using statement. d) All of the above

Answers:

b) File

b) [Link]()

b) [Link]()

a) OpenFile() or b) [Link]()
a) ReadLine() or d) ReadAllLines()

a) WriteLine() or c) WriteAllText()

d) All of the above

a) A FileStream is used to read and write bytes, while a StreamReader is used to read text.

a) The [Link]() method reads the entire contents of a file into a single string, while the
[Link]() method reads the entire contents of a file into an array of strings.

d) All of the above

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

A. Oracle
B. Microsoft
C. GNU project
D. Google

Answer: B) Microsoft

Explanation:

C# is a programming language, developed by Microsoft.

2. C# runs on the ___.

A. .NET Framework
B. Java Virtual Machine
C. Both A. and B.
D. 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 -

A. Web apps
B. Desktop apps
C. Mobiles apps
D. 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?

A. Yes
B. No

Answer: A) Yes

Explanation:

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

Discuss this Question

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

A. Yes
B. 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?


A. .c
B. .cpp
C. .cs
D. .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?

A. Anders Hejlsberg
B. Douglas Crockford
C. Rasmus Lerdorf
D. 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?

A. Yes
B. 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?

A. Yes
B. 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 ___.

A. Common Type System


B. Common Language Specification
C. Common Language Runtime
D. Java Virtual Machine

Answer: C) Common Language Runtime

Explanation:

CLR stands for Common Language Runtime.

Discuss this Question

11. .Net CLR is equivalent to?

A. Common Type System


B. Common Language Specification
C. Common Language Runtime
D. 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#?

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


B. It is a virtual machine component of JVM
C. It is a compiler to compiler the C# code
D. 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?

A. Manages the execution of Webpages programs


B. Manages the parsing of the various types
C. Manages the execution of .NET programs
D. 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 ___.

A. Simple Object Access Protocol


B. Simple Object Access Program
C. Standard Object Access Protocol
D. 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#?

A. True
B. 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?

A. Square brackets []
B. Curly braces {}
C. Round brackets ()
D. 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 ___.


A. Colon (:)
B. Semicolon (;)
C. Comma (,)
D. Dot (.)

Answer: B) Semicolon (;)

Explanation:

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

Discuss this Question

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

A. Yes
B. 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 ___.

A. Two forward slashes (//)


B. Two backward slashes (\\)
C. A hash character (#)
D. 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 ___.

A. // and //
B. \\ and //
C. <!-- and -->
D. /* 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#?

A. type variableName = value;


B. type variableName;
C. variableName as type = value;
D. 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#?

A. text
B. txt
C. string
D. 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?

A. define
B. fixed
C. constant
D. 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?

A. const type constant_name;


B. const type constant_name = value;
C. const constant_name as type;
D. 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?

A. long
B. int
C. float
D. 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#?

A. char -> int -> long -> float -> double


B. bool -> char -> int -> long -> float -> double
C. char -> int -> float -> long -> double
D. 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#?

A. double -> float -> long -> int -> char -> bool
B. double -> float -> long -> int -> char
C. float -> double -> long -> int -> char
D. 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?

A. int_variable = (int) float_variable;


B. int_variable = float_variable;
C. int_variable = (int) (float_variable);
D. 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;
[Link]([Link](x));
}
}
}

A. True
B. true
C. False
D. false

Answer: A) True

Explanation:

In the above C# code, we are using [Link]() 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;
[Link](Convert.ToInt32(x));
}
}
}

A. 10.30
B. 10.25
C. 10
D. 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#?
A. Class
B. Object
C. Method
D. 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) {
[Link]("Enter a number:");
int num = [Link]();
[Link]("Given number is: " + num);
}
}
}

A. Given number is:123


B. Given number is: 123
C. Given number is: "123"
D. Error

Answer: D) Error

Explanation:

In C#, [Link]() 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) {
[Link]("Enter a number:");
int num = Convert.ToInt32([Link]());
[Link]("Given number is: " + num);
}
}
}

A. Given number is:123


B. Given number is: 123
C. Given number is: "123"
D. Error

Answer: B) Given number is: 123

Explanation:

In C#, [Link]() 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#?

A. Parse([Link]())
B. ToSingle([Link]())
C. ToFloat([Link]());
D. Both A and B
E. Both A and C

Answer: B) Both A and B

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

[Link]([Link]());
[Link]([Link]());

Discuss this Question

35. What is the difference between [Link]() and [Link]() methods in


C#?

A. Write() writes a single character while [Link]() writes a complete line


B. Write() writes the data on the console without printing a newline while
[Link]() writes the data on the console along with printing a newline
C. ToFloat([Link]());
D. Write() writes the string on the console while [Link]() writes the string as
well as values of the variables on the console
E. Both can be used for the same purpose

Answer: B) Write() writes the data on the console without printing a newline while
[Link]() writes the data on the console along with printing a newline

Explanation:

The only difference between the [Link]() and [Link]() is that


the [Link]() is used to write data without printing the new line,
while [Link]() 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;
[Link]("{0},{0}", a, b);
}
}
}
A. 10,10
B. 10,20
C. 20,20
D. 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;
[Link]("{0}+{1}", a, b);
}
}
}

A. 20
B. 30
C. 10+20
D. 10+10

Answer: C) 10+20

Explanation:

In the statement, [Link]("{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) {
[Link]("True");
} else {
[Link]("False");
}
}
}

A. True
B. False
C. Error
D. 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) {
[Link](true ^ true);
}
}

A. True
B. False
C. Error
D. 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) {
[Link](true && false);
}
}

A. True
B. False
C. Error
D. 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#?

A. do; {
B. statement(s);
C. } while (test_condition);
D.
E. do
F. {
G. statement(s);
H. } while (test_condition)
I.
J. do(test_condition) {
K. statement(s);
L. }while;
M.
N. do {
O. statement(s);
P. } while (test_condition);
Q.

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?


A. Yes
B. 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) {


[Link](elements);
}
}
}
/*
Output:
10
20
30
40
50
*/

Discuss this Question

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

A. foreach(type variable_name in collection_name); {


B. statement(s);
C. }
D.
E. foreach(type variable_name in collection_name) {
F. statement(s);
G. }
H.
I. foreach(variable_name As Type in collection_name){
J. statement(s);
K. }
L.
M. foreach(dim type variable_name in collection_name) {
N. statement(s);
O. }
P.

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 {
[Link](i + " ");
++i;
} while (i <= 50);
}
}

A. Error
B. 100 101 102 ... Infinite
C. 101
D. 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;
[Link](i++);
}
}

A. 10
B. 11
C. 12
D. Error

Answer: A) 10

Explanation:

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

Discuss this Question

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


A. for(;;)
B. for(;;);
C. for(;;)
D. 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#?

A. Yes
B. 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?

A. Yes
B. 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() {
[Link]("In the main...");
int i = 1;

okay:
[Link](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#?

A. to return from the calling functions to called function


B. to throw an exception manually during the execution of the program
C. to return from the switch statement
D. 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?

A. break
B. continue
C. Both A and B
D. 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. A variable
B. A Class
C. An Array
D. An object

Answer: D) An object

Explanation:

In C#, String is an object of [Link] class.

Discuss this Question

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

A. Yes
B. 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?

A. String
B. [Link]
C. [Link]
D. [Link]

Answer: D) [Link]

Explanation:

In C#, [Link] 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?

A. !=
B. !
C. ~
D. ~

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?

A. Equals()
B. Compare()
C. Both A and B
D. 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?

A. Upper()
B. ToUpper()
C. Upr()
D. 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";
[Link]([Link]());
}
}

Discuss this Question


57. What is [Link] in C#?

A. Property
B. Method
C. Constructor
D. Both A and B

Answer: A) Property

Explanation:

The [Link] is property of [Link] class that is used to get the number of
characters in the current String object.

Syntax of [Link] 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;
[Link]([Link](s3) + " " + [Link](s1));
}
}

A. Error
B. True True
C. True False
D. True 1

Answer: D) True 1

Explanation:
The Equals() method returns a bool value. We assigned s1 to s3. Thus, the
statement [Link](s3) will returns "True". And, the CompareTo() method returns an
int value (Less than 0, o, or greater than 0). The statement [Link](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";
[Link]([Link]);

}
}

A. 5
B. 6
C. 7
D. 4

Answer: A) 5

Explanation:

The [Link] 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";
[Link]([Link]('h'));
}
}

A. 0
B. 1
C. -1
D. 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 ___.

A. Square brackets []
B. Curly brackets {}
C. Round brackets ()
D. 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"};
[Link](mobiles);
}
}
}

A. "iPhone", "Samsung", "Vivo"


B. {"iPhone", "Samsung", "Vivo"}
C. string[]
D. [Link][]

Answer: D) [Link][]

Explanation:

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

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"};
[Link](mobiles[-1]);
}
}
}

A. None
B. Warning
C. Exception
D. [Link][]

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 "[Link]: 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};

A. 'x' is a reference to the array created on stack


B. 'x' is a reference to an object created on stack
C. 'x' is a reference to an object of a '[Link]' class
D. None of the above

Answer: C) 'x' is a reference to an object of a '[Link]' class

Explanation:

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

Discuss this Question

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

A. Len
B. Length
C. Elements
D. 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"};
[Link](mobiles[0] + mobiles[2]);
}
}
}

A. iPhoneVivo
B. iPhone+Vivo
C. Exception
D. 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#?

A. sort()
B. sorting()
C. Sort()
D. 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"};
[Link](mobiles[0] + mobiles[2]);
}
}
}

A. iPhone
B. Vivo
C. iPhoneVivo
D. 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}};
[Link]([Link](0)+","+[Link](1));
}
}
}

A. 4,4
B. 2,2
C. Compilation Error
D. 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#?

A. int arr[2][3] = new int[2][3];


B. int arr[2,3] = new int[2,3];
C. int[,] arr = new int[2,3];
D. 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#?

A. Class
B. class
C. [Link]
D. [Link]

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#?

A. Class_Name Object_Name = new Class_Name();


B. Class_Name Object_Name;
C. new Object_Name as Class_Name();
D. 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();
[Link]([Link]);
}
}
}

// Output: Apple

Discuss this Question

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


A. Arrow Operator (->)
B. Dot Operator (.)
C. Greater Than (>)
D. 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#?

A. Yes
B. 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#?

A. Static Constructor
B. Private Constructor
C. Body Constructor
D. 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#?

A. 2
B. 3
C. 4
D. 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#?

A. To maintain the syntax


B. To define a variable inside the class
C. To access the variables defined inside the class
D. 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 ___.

A. Types and type members


B. Defining a field that can be accessed in all classes
C. Defining a field that can be accessed in inherited classes
D. 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 ___.

A. its class and all other classes


B. its class and by derived class instances
C. its class only
D. 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?


A. Yes
B. 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?

A. Polymorphism
B. Encapsulation
C. Abstraction
D. 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) {
[Link](x + ", " + Class1.x);
}
}
}

A. 20, 20
B. 10, 10
C. 20, 10
D. 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?

A. overrides
B. protected
C. base
D. 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#?

A. interface
B. Interface
C. implement
D. 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 ___.

A. virtual
B. sealed
C. private
D. 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 ___.

A. Cache Memory
B. Stack Memory
C. Heap Memory
D. 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#?

A. Methods
B. Properties
C. Events
D. 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#?

A. private
B. public
C. protected
D. 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#?

A. Abstract Class
B. Static Class
C. Sealed Class
D. 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#?

A. Overloads
B. New
C. Overrides
D. 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?

A. Yes
B. 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.


A. Value
B. Memory address
C. Size of the type
D. 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 ___.

A. value types and arrays


B. reference types
C. pointer types
D. 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#?

A. One
B. Two
C. Many
D. None of the above

Answer: C) Many

Explanation:

In C#, there can be multiple (many) catch blocks with a single try block.
95. Is the use of return statement necessary in every function in C#?

A. Yes
B. No

Answer: A) Yes

Explanation:

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

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

A. private
B. public
C. protected
D. 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.

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

A. True
B. False

Answer: A) True

Explanation:

Yes, a namespace is the collection of classes.

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

A. System.Int16
B. System.Int32
C. System.UInt16
D. System.UInt32

Answer: D) System.UInt32

Explanation:

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

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

A. using System;
B. using system;
C. using Namespace;
D. namespace MyApplication

Answer: A) using System;

Explanation:

The first line of a C# program is:

using System;

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

A. Lightweight
B. Darkweight
C. Extra
D. Compress

Answer: A) Lightweight

Explanation:

In C#, structures are used for lightweight objects such as mobiles, cars, etc.
1. Which namespace is fundamental for basic
C# operations?
a) using CSharp;
b) using [Link];
c) using System;
d) using Base;

Click to View Answer and Explanation

Answer:
c) using System;

Explanation:

The System namespace contains fundamental classes and base classes that
define commonly used value and reference data types, events and event
handlers, interfaces, attributes, and processing exceptions.

2. What is the [Link]() function do?


a) Reads input
b) Writes to a file
c) Writes to the console with a new line
d) Clears the console

Click to View Answer and Explanation

Answer:
c) Writes to the console with a new line

Explanation:

The [Link]() function is used to display a message on the


console and then move to the next line.

3. Which keyword is used to create an


instance of a class?
a) class
b) new
c) struct
d) this

Click to View Answer and Explanation

Answer:
b) new

Explanation:

The new keyword is used to create an instance of a class, also known as an


object.

4. Which of the following data types can


store a non-integer number?
a) int
b) char
c) double
d) bool

Click to View Answer and Explanation

Answer:
c) double

Explanation:

The double data type is used to store floating-point numbers (numbers with
decimal points).

5. What does the static keyword mean in C#?


a) The variable can change
b) The method can be overridden
c) The member belongs to the type rather than any specific instance
d) The value can be assigned only once

Click to View Answer and Explanation

Answer:
c) The member belongs to the type rather than any specific instance
Explanation:

A static member belongs to the class/type itself rather than a specific


instance.

6. What is the purpose of the Main method in


C#?
a) To define variables
b) To serve as the entry point for the application
c) To close the application
d) To initialize objects

Click to View Answer and Explanation

Answer:
b) To serve as the entry point for the application

Explanation:

The Main method is the primary entry point for a C# console application.

7. Which of the following is NOT a valid


access modifier in C#?
a) private
b) public
c) transient
d) protected

Click to View Answer and Explanation

Answer:
c) transient

Explanation:

transient is not a valid access modifier in C#. It's used in Java for other
purposes.

8. What is encapsulation?
a) Breaking the code into small functions
b) Wrapping up data members and methods into a single unit
c) Inheriting properties of one class into another
d) Implementing multiple interfaces

Click to View Answer and Explanation

Answer:
b) Wrapping up data members and methods into a single unit

Explanation:

Encapsulation refers to the bundling of data and methods that operate on


that data within a single unit, typically a class.

9. Which keyword is used to inherit from a


base class in C#?
a) extends
b) inherits
c) implement
d) :

Click to View Answer and Explanation

Answer:
d) :

Explanation:

In C#, the : symbol is used to indicate inheritance from a base class or


implementation of an interface.

10. Which type of exception handling uses a


try-catch block?
a) Unchecked
b) Implicit
c) Explicit
d) Checked

Click to View Answer and Explanation


Answer:
d) Checked

Explanation:

Checked exceptions are handled using try-catch blocks. These are the
exceptions that are checked at compile time.

11. Which of the following is NOT a loop


structure in C#?
a) for
b) while
c) loop
d) foreach

Click to View Answer and Explanation

Answer:
c) loop

Explanation:

C# does not have a loop structure called loop.

12. Which operator is used for type casting in


C#?
a) ::
b) as
c) cast
d) ->

Click to View Answer and Explanation

Answer:
b) as

Explanation:

The as operator is used for type casting in C#. If the conversion is not
possible, it returns null.
13. Which of the following is the correct way
to comment out multiple lines in C#?
a) // ...
b) /* ... */
c) -- ... --
d)

Click to View Answer and Explanation

Answer:
b) /* ... */

Explanation:

The /* ... */ syntax is used to comment out multiple lines in C#.

14. Which keyword is used to declare a


constant variable in C#?
a) constant
b) con
c) const
d) final

Click to View Answer and Explanation

Answer:
c) const

Explanation:

In C#, the const keyword is used to declare a variable as constant, meaning


its value cannot be changed after initialization.

15. Which of the following data types can


store a Unicode character in C#?
a) char
b) string
c) byte
d) short

Click to View Answer and Explanation

Answer:
a) char

Explanation:

The char data type in C# is used to store a single Unicode character.

16. How do you define a method in C# that


does not return a value?
a) void
b) null
c) empty
d) returnless

Click to View Answer and Explanation

Answer:
a) void

Explanation:

In C#, the void keyword indicates that a method does not return a value.

17. Which of the following statements about


C# interfaces is TRUE?
a) They can contain the implementation of methods.
b) They can be instantiated.
c) They can have fields.
d) They can declare methods without implementations.

Click to View Answer and Explanation

Answer:
d) They can declare methods without implementations.
Explanation:

Interfaces in C# can only declare methods, properties, events, or indexers


without providing the actual implementation.

18. Which of the following access modifiers


allows a member to be accessed from any
class within the same assembly but not from
outside the assembly?
a) private
b) public
c) protected
d) internal

Click to View Answer and Explanation

Answer:
d) internal

Explanation:

The internal access modifier allows a member to be accessible from any


class within the same assembly, but not from outside that assembly.

19. Which of the following is NOT a type of


constructor in C#?
a) Copy constructor
b) Static constructor
c) Default constructor
d) Derived constructor

Click to View Answer and Explanation

Answer:
d) Derived constructor

Explanation:

There's no concept of a "Derived constructor" in C#. Constructors in derived


classes are just constructors that can call the base class constructor.
20. What does the ?? operator do in C#?
a) Compares two values
b) Assign one of two values depending on a condition
c) Checks for nullability and returns the non-null value
d) Merges two values

Click to View Answer and Explanation

Answer:
c) Checks for nullability and returns the non-null value

Explanation:

The ?? is called the null-coalescing operator. It returns the left-hand operand


if it is not null; otherwise, it returns the right-hand operand.

21. What is boxing in C#?


a) Placing a value type inside an object
b) Extracting a value type from an object
c) Creating an array of objects
d) Defining multiple boxes for UI design

Click to View Answer and Explanation

Answer:
a) Placing a value type inside an object

Explanation:

Boxing is the process of converting a value type to the type object or any
interface type implemented by this value type.

22. What does the override keyword do in C#?


a) Hides the base class method
b) Makes a method static
c) Provides a new implementation for a virtual method in a derived class
d) Makes a method abstract

Click to View Answer and Explanation


Answer:
c) Provides a new implementation for a virtual method in a derived class

Explanation:

The override keyword is used in a derived class to provide a new


implementation for a method that is declared as virtual in the base class.

23. Which collection in C# does NOT allow


duplicate elements?
a) List
b) Array
c) Hashtable
d) HashSet

Click to View Answer and Explanation

Answer:
d) HashSet

Explanation:

HashSet is designed to store unique elements, and it doesn't allow


duplicates.

24. How do you declare a single-dimensional


array in C#?
a) int x[];
b) int x();
c) int[] x;
d) int{} x;

Click to View Answer and Explanation

Answer:
c) int[] x;

Explanation:

The correct way to declare a single-dimensional array in C# is int[] x;.


25. What is LINQ in C#?
a) A collection of functions
b) A new programming language
c) A library for parallel programming
d) A framework for querying data in a consistent manner

Click to View Answer and Explanation

Answer:
d) A framework for querying data in a consistent manner

Explanation:

LINQ (Language Integrated Query) is a Microsoft .NET Framework component


that adds native data querying capabilities to .NET languages.

26. Which of the following statements


correctly initializes a string to an empty
value?
a) string str = null;
b) string str = "";
c) string str = " ";
d) string str = [Link];

Click to View Answer and Explanation

Answer:
d) string str = [Link];

Explanation:

Both b) and d) initialize the string to an empty value, but [Link] is a


more readable way of representing an empty string in C#.

27. What does the finally block in exception


handling do?
a) Catches exceptions
b) Throws exceptions
c) Executes regardless of whether an exception is thrown or caught
d) Stops exceptions from propagating

Click to View Answer and Explanation

Answer:
c) Executes regardless of whether an exception is thrown or caught

Explanation:

The finally block is used to execute code, regardless of whether an


exception is thrown or caught. It is typically used for cleanup activities, such
as closing files or releasing resources.

28. Which of the following keywords is used


to declare a constant variable?
a) const
b) readonly
c) final
d) static

Click to View Answer and Explanation

Answer:
a) const

Explanation:

The const keyword in C# is used to declare a constant variable. Once


assigned, its value cannot be changed.

29. Which of the following methods is used to


determine the length of a string in C#?
a) Length()
b) Size()
c) Count()
d) Length

Click to View Answer and Explanation


Answer:
d) Length

Explanation:

In C#, the Length property of the string class is used to get the number of
characters in a string.

30. What is the base class for all classes in


C#?
a) Object
b) Base
c) Root
d) System

Click to View Answer and Explanation

Answer:
a) Object

Explanation:

The Object class is the base class for all classes in C#.

31. Which of the following is NOT a valid


access modifier in C#?
a) public
b) private
c) protected
d) secured

Click to View Answer and Explanation

Answer:
d) secured

Explanation:

secured is not a valid access modifier in C#. The valid access modifiers
are public, private, protected, internal, and a few combinations thereof.
32. Which of the following types is a
reference type?
a) int
b) char
c) bool
d) string

Click to View Answer and Explanation

Answer:
d) string

Explanation:

string is the reference type in C#.

33. In C#, what does the is keyword do?


a) Checks type compatibility
b) Performs a deep copy of objects
c) Implements inheritance
d) Compares two strings

Click to View Answer and Explanation

Answer:
a) Checks type compatibility

Explanation:

The is keyword is used in C# to check if an object is of a certain type or


compatible with that type.

34. How can you make a class in C# so that it


cannot be inherited?
a) final
b) static
c) sealed
d) abstract
Click to View Answer and Explanation

Answer:
c) sealed

Explanation:

In C#, the sealed keyword prevents a class from being inherited.

35. Which of the following is the correct way


to define an anonymous method in C#?
a) delegate void() { }
b) func void() { }
c) delegate() { }
d) delegate void { }

Click to View Answer and Explanation

Answer:
c) delegate() { }

Explanation:

Anonymous methods in C# are created using the delegate keyword followed


by the method body.

36. Which of the following correctly declares


a nullable int in C#?
a) int? x;
b) nullable int x;
c) int x?;
d) int x = null;

Click to View Answer and Explanation

Answer:
a) int? x;

Explanation:

In C#, the ? modifier is used to declare a nullable value type.


37. Which of the following is NOT a valid C#
collection?
a) ArrayList
b) HashTable
c) LinkList
d) Queue

Click to View Answer and Explanation

Answer:
c) LinkList

Explanation:

While LinkedList is a valid collection in C#, there's no collection named


LinkList.

38. Which method is called to free the


resources of an object manually in C#?
a) delete()
b) free()
c) remove()
d) Dispose()

Click to View Answer and Explanation

Answer:
d) Dispose()

Explanation:

The Dispose() method is used to release unmanaged resources in C#.

39. Which of the following keywords is used


to handle an exception in C#?
a) catch
b) throw
c) error
d) handle

Click to View Answer and Explanation

Answer:
a) catch

Explanation:

In C#, the try-catch block is used for exception handling, where the catch
block catches and handles exceptions.

40. What does the as keyword do in C#?


a) It performs a deep copy of objects
b) It checks for type compatibility and returns null if the conversion is not possible
c) It compares two strings
d) It enforces inheritance

Click to View Answer and Explanation

Answer:
b) It checks for type compatibility and returns null if the conversion is not possible

Explanation:

The as keyword in C# performs a type conversion, but instead of throwing an


exception, it returns null if the conversion isn't possible.

41. Which of the following types can be used


to declare a variable that can range from -
2,147,483,648 to 2,147,483,647?
a) long
b) short
c) int
d) byte

Click to View Answer and Explanation

Answer:
c) int
Explanation:

In C#, the int data type is a 32-bit signed integer that can range from -
2,147,483,648 to 2,147,483,647.

42. Which of the following keywords is used


to declare an abstract class in C#?
a) abstracted
b) abs
c) abstract
d) abstraction

Click to View Answer and Explanation

Answer:
c) abstract

Explanation:

The abstract keyword is used in C# to declare an abstract class. Abstract


classes cannot be instantiated.

43. Which of the following methods can be


overridden in a derived class?
a) Static method
b) Virtual method
c) Sealed method
d) Private method

Click to View Answer and Explanation

Answer:
b) Virtual method

Explanation:

In C#, only methods declared with the virtual keyword can be overridden in
a derived class.

44. What is the default value of a bool in C#?


a) true
b) false
c) null
d) 0

Click to View Answer and Explanation

Answer:
b) false

Explanation:

In C#, the default value for a bool (boolean) data type is false.

45. Which collection in C# ensures that all


elements are unique?
a) List
b) Dictionary
c) Queue
d) HashSet

Click to View Answer and Explanation

Answer:
d) HashSet

Explanation:

The HashSet collection in C# ensures that all elements are unique. It does not
allow duplicate values.

46. Which operator can be used to determine


the type of an object at runtime?
a) is
b) typeof
c) sizeof
d) type

Click to View Answer and Explanation


Answer:
b) typeof

Explanation:

The typeof operator is used to obtain the Type object for a type. However, to
check an object's type at runtime, the is operator is more commonly used.

47. Which of the following statements about


delegates in C# is true?
a) Delegates can point to instance methods only.
b) Delegates cannot be used with static methods.
c) Delegates are type-safe.
d) Delegates can't be used with lambda expressions.

Click to View Answer and Explanation

Answer:
c) Delegates are type-safe.

Explanation:

Delegates in C# are type-safe. They ensure the method signature matches


the delegate signature. Delegates can point to both instance and static
methods and can also be used with lambda expressions.

48. Which of the following provides a way to


have multiple implementations for a method?
a) Overloading
b) Overriding
c) Hiding
d) Abstracting

Click to View Answer and Explanation

Answer:
a) Overloading
Explanation:

Method overloading allows a class to have multiple methods with the same
name but with different parameters.

49. What does the virtual keyword indicate?


a) The method cannot be overridden in the derived class.
b) The method must be overridden in the derived class.
c) The method can be overridden in the derived class.
d) The method is abstract.

Click to View Answer and Explanation

Answer:
c) The method can be overridden in the derived class.

Explanation:

The virtual keyword in C# indicates that a method, property, indexer, or


event can be overridden in derived classes.

50. Which of the following statements


correctly creates an array of 5 integers?
a) int[] array = new int[5];
b) int[] array = 5;
c) int array[5];
d) int array = new int[5];

Click to View Answer and Explanation

Answer:
a) int[] array = new int[5];

Explanation:

In C#, arrays are declared by specifying the type of the elements followed by
square brackets. They are then instantiated using the new keyword followed
by the type and the size of the array.
Sure, here are some C# database programming interview questions in multiple-choice format:

Question 1:

Which of the following is not a valid data type in C#?

(a) int

(b) double

(c) string

(d) varchar

Answer: (d) varchar

Question 2:

What is the purpose of the namespace keyword in C#?

(a) To organize code into logical groups

(b) To prevent naming conflicts

(c) To add comments to code

(d) To create new classes

Answer: (a) To organize code into logical groups

Question 3:

What is the difference between a class and an object?

(a) A class is a blueprint for creating objects.

(b) An object is an instance of a class.

(c) Both are the same thing.

(d) None of the above.

Answer: (a) A class is a blueprint for creating objects.

Question 4:

What is the purpose of the using keyword in C#?

(a) To manage the lifetime of resources

(b) To prevent memory leaks

(c) To improve code performance


(d) To make code more readable

Answer: (a) To manage the lifetime of resources

Question 5:

What is the difference between [Link] and Entity Framework?

(a) [Link] is a low-level API for accessing databases, while Entity Framework is a higher-level API that
provides more abstraction.

(b) [Link] is a data access technology for .NET applications, while Entity Framework is an object-
relational mapper (ORM) that simplifies data access.

(c) [Link] is a component of the .NET Framework, while Entity Framework is a third-party library.

(d) All of the above.

Answer: (d) All of the above.

Question 6:

What is the purpose of the SqlConnection class in [Link]?

(a) To establish a connection to a database

(b) To execute SQL commands

(c) To retrieve data from a database

(d) All of the above

Answer: (a) To establish a connection to a database

Question 7:

What is the purpose of the SqlCommand class in [Link]?

(a) To execute SQL commands

(b) To establish a connection to a database

(c) To retrieve data from a database

(d) None of the above

Answer: (a) To execute SQL commands

Question 8:

What is the purpose of the SqlDataReader class in [Link]?

(a) To execute SQL commands

(b) To establish a connection to a database


(c) To retrieve data from a database

(d) None of the above

Answer: (c) To retrieve data from a database

Question 9:

What is the purpose of the SqlParameter class in [Link]?

(a) To specify parameters for SQL commands

(b) To execute SQL commands

(c) To establish a connection to a database

(d) To retrieve data from a database

Answer: (a) To specify parameters for SQL commands

Question 10:

What is the purpose of the Entity Framework DbContext class?

(a) To manage the lifetime of Entity Framework objects

(b) To execute SQL commands

(c) To establish a connection to a database

(d) To retrieve data from a database

Answer: (a) To manage the lifetime of Entity Framework objects

You might also like