C# Multiple Choice Questions
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
B
What is the default value of a bool in C#? a) true b) false c) null d) 0
B
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
B
Which keyword is used to create an instance of a class? a) class b) new c) struct d) this
B
Double a = 8; int b = (int) a; Console.WriteLine(b) The above is, a) an explicit type conversion. b) an implicit type conversion. c) a normal type conversion. d) not going to work.
A
Greetings(Console.WriteLine("Hello!")) Main(Greetings()) In the above code, a) Greetings is a function without parameters. b) Greetings is a function, but produces nothing. c) Greetings is a function that is not callable from the Main program. d) Greetings is a function that does not produce anything because of the void keyword.
A
How do you define a method in C# that does not return a value? a) void b) null c) empty d) returnless
A
Which of the following is the correct way to comment out multiple lines in C#? a) // ... b) /* ... */ c) -- ... -- d)
B
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
A
In C#, you can create multiple functions with the same name provided that a) there are different parameters. b) the void key word is used. c) return type is not different. d) return type is not specified.
A
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
A
What is the base class for all classes in C#? a) Object b) Base c) Root d) System
A
Which of the following data types can store a Unicode character in C#? a) char b) string c) byte d) short
A
Which of the following keywords is used to declare a constant variable? a) const b) readonly c) final d) static
A
Which of the following keywords is used to handle an exception in C#? a) catch b) throw c) error d) handle
A
Which of the following provides a way to have multiple implementations for a method? a) Overloading b) Overriding c) Hiding d) Abstracting
A
Which of the following methods can be overridden in a derived class? a) Static method b) Virtual method c) Sealed method d) Private method
B
Which operator can be used to determine the type of an object at runtime? a) is b) typeof c) sizeof d) type
B
Which operator is used for type casting in C#? a) :: b) as c) cast d) ->
B
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];
A
int x = 5 string y = (x%2 == 0)?"Its Even":"Its Odd" Console.WriteLine(y) The above code snippet, when run, will produce, a) It's odd. b) It's even. c) neither. d) an error.
A
int a = 8 double b = a Console.WriteLine(b) The above code will produce, a) an error b) 8 c) 8.0 d) None of the above.
B
string [] cities = {....}, a) is a string. b) is a string array. c) a generic array. d) an array of any type.
B
A certain function was declared in C# with a single parameter. If it was called without giving an argument, when in use, a) it will definitely produce an error. b) it would not produce an error if a default parameter was given. c) it would produce an error depending on the type of the parameter. d) it would not produce an error if the parameter was of string type.
B
Donald John Trump must be taken from a database, to be printed as the full name of the former US president. The easiest way to achieve this is, a) using three string variables for the first name, middle name and the surname and then add them. b) using string.Concat() function. c) using string.concat() function. d) b or c.
B
In the statement, static void Main(string args []), a) Main is a normal function that returns a value. b) Main is normal function that does not return a value. c) Main is a normal function that can be called in any part of the program. d) The word, static, does not restrict the use of the function just inside the class that it belongs to.
B
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
B
Console.ReadLine(), a) is only for displaying content on a screen. b) is only for alerting a user. c) can be used to get user input. d) none of the above is true.
C
How can you make a class in C# so that it cannot be inherited? a) final b) static c) sealed d) abstract
C
How do you declare a single-dimensional array in C#? a) int x[]; b) int x(); c) int[] x; d) int{} x;
C
In order to store π in a variable in an efficient manner, a) use a string variable with π within single quotes. b) use a string variable with π within double quotes. c) use a char variable with π within single quotes. d) use either b or c.
C
To comment out a line of code in C#, a) use # at the front. b) use # at the end. c) use // at the front. d) use /* at the front.
C
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
C
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
C
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
C
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
C
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.
C
What is the Console.WriteLine() function do? a) Reads input b) Writes to a file c) Writes to the console with a new line d) Clears the console
C
Which keyword is used to declare a constant variable in C#? a) constant b) con c) const d) final
C
Which namespace is fundamental for basic C# operations? a) using CSharp; b) using System.Collections; c) using System; d) using Base;
C
Which of the following data types can store a non-integer number? a) int b) char c) double d) bool
C
Which of the following is NOT a loop structure in C#? a) for b) while c) loop d) foreach
C
Which of the following is NOT a valid C# collection? a) ArrayList b) HashTable c) LinkList d) Queue
C
Which of the following is NOT a valid access modifier in C#? a) private b) public c) transient d) protected
C
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 { }
C
Which of the following keywords is used to declare an abstract class in C#? a) abstracted b) abs c) abstract d) abstraction
C
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.
C
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
C
int x =25 double y = Console.WriteLine(y) If the above code snippet produces 5, the highlighted code could be, a) Math.Sqrt() b) Math.Pow(x,2). c) Math.Pow(x,0.5) d) x**0.5
C
string prime [] = {2,3,5,7,11,13,19}. To find the size of the array, a) prime.length can be used. b) prime.length() can be used. c) prime.Length can be used. d) prime.Length() can be used.
C
Console.WriteLine() and Console.Write(), a) produce the same output b) the latter produces an output, followed by a line break. c) the former gives clarity to the code. d) the former produces an output, followed by a line break
D
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
D
Which collection in C# does NOT allow duplicate elements? a) List b) Array c) Hashtable d) HashSet
D
Which collection in C# ensures that all elements are unique? a) List b) Dictionary c) Queue d) HashSet
D
Which keyword is used to inherit from a base class in C#? a) extends b) inherits c) implement d) :
D
Which method is called to free the resources of an object manually in C#? a) delete() b) free() c) remove() d) Dispose()
D
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
D
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;
D
Which of the following is NOT a type of constructor in C#? a) Copy constructor b) Static constructor c) Default constructor d) Derived constructor
D
Which of the following is NOT a valid access modifier in C#? a) public b) private c) protected d) secured
D
Which of the following methods is used to determine the length of a string in C#? a) Length() b) Size() c) Count() d) Length
D
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.
D
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 = String.Empty;
D
Which of the following types is a reference type? a) int b) char c) bool d) string
D
Which statement is true about break in a program? a) It cannot be used in a Switch statement. b) It cannot be used in a for loop. c) It cannot be used in a while loop. d) It can be used in a for-loop or while loop.
D
Which type of exception handling uses a try-catch block? a) Unchecked b) Implicit c) Explicit d) Checked
D
int a = 8 double b = a This is... a) a type conversion. b) a code snippet that is not workable. c) an explicit conversion d an implicit conversion
D
int i = 5 While(i <= 5) (string a = 2 + " times " + i + " = " + 2*i In order to produce the above output, the highlighted line should be, a) i = i + 1; b) i++; c) i+=1; d) any of the above.
D
int x = 23, y = 23 x+=1 y++ Console.WriteLine(x + "," + y) The above code snippet will produce, a) an error. b) 23 , 24. c) 24 , 23. d) 24 , 24.
D
int x = 9; if(x%3 == 0) (Console.WriteLine()) else(Console.WriteLine()) What could the else statement be, a) It's not an even number. b) It's a multiple of three. c) It's not an odd number. d) It's not a multiple of three.
D