0% found this document useful (0 votes)
3 views

C# Basics

The document contains a series of questions and answers related to C# programming concepts, including method definitions, object creation, keywords, exception handling, and data types. It covers topics such as the 'readonly' keyword, differences between 'IEnumerable' and 'IQueryable', and the purpose of 'using', 'lock', and 'yield' keywords. Additionally, it includes programming tasks and questions about access modifiers and data types.

Uploaded by

saksham.kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C# Basics

The document contains a series of questions and answers related to C# programming concepts, including method definitions, object creation, keywords, exception handling, and data types. It covers topics such as the 'readonly' keyword, differences between 'IEnumerable' and 'IQueryable', and the purpose of 'using', 'lock', and 'yield' keywords. Additionally, it includes programming tasks and questions about access modifiers and data types.

Uploaded by

saksham.kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

In C#, how do you define a method that does not return any value?

a) void myMethod()

b) int myMethod()

c) double myMethod()

d) string myMethod()

Answer: a) void myMethod()

What is the correct syntax to create an object of a class in C#?

a) ClassName obj = new ClassName();

b) new ClassName obj = ClassName();

c) obj ClassName = new ClassName();

d) ClassName obj = ClassName;

Answer: a) ClassName obj = new ClassName();

Which C# keyword is used to create an alias for a data type?

a) new

b) var

c) using

d) alias

Answer: b) var

What is the purpose of the "readonly" keyword in C#?

a) To declare a constant variable that can be modified later in the code.

b) To declare a variable that can be accessed from any class.

c) To indicate that a method does not modify the state of an object.

d) To declare a variable that cannot be modified after initialization.

Answer: d) To declare a variable that cannot be modified after initialization.

Which C# statement is used to handle exceptions in code?

a) try
b) catch

c) exception

d) handle

Answer: b) catch

What is the purpose of the "async" and "await" keywords in C#?

a) To define a method that can be invoked asynchronously.

b) To create an instance of a class in an asynchronous manner.

c) To handle exceptions in asynchronous code.

d) To declare a method that returns a Task<T> for asynchronous operations.

Answer: d) To declare a method that returns a Task<T> for asynchronous operations.

In C#, what is the difference between "IEnumerable" and "IQueryable"?

a) "IEnumerable" is used for querying in-memory collections, while "IQueryable" is used for querying
databases.

b) "IEnumerable" is a synchronous interface, while "IQueryable" is an asynchronous interface.

c) "IEnumerable" supports deferred execution, while "IQueryable" does not.

d) "IEnumerable" is used for querying databases, while "IQueryable" is used for querying in-memory
collections.

Answer: a) "IEnumerable" is used for querying in-memory collections, while "IQueryable" is used for
querying databases.

What is the purpose of the "using" statement in C# when working with resources?

a) To explicitly release resources after their usage.

b) To automatically release resources when they go out of scope.

c) To allocate memory for resources on the heap.

d) To prevent resource leakage in the application.

Answer: b) To automatically release resources when they go out of scope.

In C#, what is the difference between a value type and a reference type?

a) Value types are stored on the heap, while reference types are stored on the stack.
b) Value types can be null, while reference types cannot.

c) Value types are passed by reference, while reference types are passed by value.

d) Value types store the actual data, while reference types store the memory address of the data.

Answer: d) Value types store the actual data, while reference types store the memory address of the
data.

What is the purpose of the "lock" keyword in C#?

a) To prevent a method from being overridden in a derived class.

b) To synchronize access to shared resources in a multi-threaded environment.

c) To define a constant value that cannot be modified.

d) To throw an exception and terminate the program.

Answer: b) To synchronize access to shared resources in a multi-threaded environment.

In C#, what is the difference between "is" and "as" operators?

a) "is" operator checks if an object can be cast to a given type, while "as" operator performs the
actual casting.

b) "is" operator checks for object equality, while "as" operator checks for reference equality.

c) "is" operator performs explicit casting, while "as" operator performs implicit casting.

d) "is" operator is used for value types, while "as" operator is used for reference types.

Answer: a) "is" operator checks if an object can be cast to a given type, while "as" operator performs
the actual casting.

What is the purpose of the "yield" keyword in C# when used with iterators?

a) To create a custom exception class.

b) To signal the end of an iteration.

c) To specify a custom comparer for sorting.

d) To return elements from an iterator method one at a time.

Answer: d) To return elements from an iterator method one at a time.

What is the role of the "EntryPoint" method in a C# program?

a) It is the main method of the program where execution starts.

b) It is used to define the application's entry point for the operating system.
c) It is used to specify command-line arguments for the program.

d) It is a reserved keyword and must be used in every C# program.

Answer: a) It is the main method of the program where execution starts.

How can you prevent a class from being inherited by other classes in C#?

a) By declaring the class as "static."

b) By declaring the class as "sealed."

c) By using the "protected" access modifier for all members of the class.

d) By declaring the class as "abstract."

Answer: b) By declaring the class as "sealed."

What is a delegate in C#?

a) A delegate is a reference type that can be used to encapsulate a method.

b) A delegate is a value type used to store memory addresses.

c) A delegate is a data type used to store large amounts of binary data.

d) A delegate is a class used to implement multiple inheritance in C#.

Answer: a) A delegate is a reference type that can be used to encapsulate a method.

Write a C# program to check if a given number is prime or not.

Write a C# program to find the sum of all even numbers from 1 to N.

Given an array of integers, write a C# program to find the maximum and minimum values in the
array.

Write a C# program to reverse a string without using any built-in functions.

Write a C# program to find the second highest number in an array of integers.

Given a sentence, write a C# program to count the number of words in the sentence.
Write a C# program to find the factorial of a given number using recursion.

Write a C# program to check if a given string is a palindrome or not.

Given two sorted arrays, write a C# program to merge them into a single sorted array.

Write a C# program to find the area of a triangle given the lengths of its three sides using Heron's
formula.

What is C#?

a) A markup language for creating web pages

b) A programming language for building Windows applications

c) A database management system

d) A scripting language for web development

Answer: b) A programming language for building Windows applications

Which of the following access modifiers allows a class member to be accessed from anywhere in the
same assembly?

a) private

b) protected

c) internal

d) public

Answer: c) internal

What does the "new" keyword do in C#?

a) It creates a new instance of a class.

b) It allocates memory for a new object on the heap.

c) It hides the base class method with a new implementation.

d) It creates a new namespace in the code.

Answer: c) It hides the base class method with a new implementation.

Which C# data type is used to store whole numbers without decimal points?
a) float

b) double

c) decimal

d) int

Answer: d) int

What is the purpose of the "using" statement in C#?

a) To import namespaces into the code

b) To declare variables

c) To create instances of classes

d) To define loops

Answer: a) To import namespaces into the code

You might also like