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

#5 Pract C#

The document contains a series of 51 questions and answers related to C# programming concepts, covering topics such as output of code snippets, method overriding, array definitions, access modifiers, and data types. Each question is followed by multiple-choice options, with the correct answer provided. The content serves as a quiz or study guide for individuals looking to test their knowledge of C#.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views15 pages

#5 Pract C#

The document contains a series of 51 questions and answers related to C# programming concepts, covering topics such as output of code snippets, method overriding, array definitions, access modifiers, and data types. Each question is followed by multiple-choice options, with the correct answer provided. The content serves as a quiz or study guide for individuals looking to test their knowledge of C#.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Question 1: 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);

• a. 20

• b. 30

• c. 10+20

• d. 10+10

Answer: c. 10+20

Question 2: Which modifier is used while redefining an abstract method by a derived class in C#?

• a. Overloads

• b. New

• c. Override

• d. Virtual

Answer: c. Override

Question 3: Method parameters are enclosed in parentheses and are separated by

• a. :

• b. :

• c. ,

• d. .

Answer: c. ,
Question 4: Which is the right way to define an array?

• a. int intArray[] = new int[5];

• b. int[] intArray = new int[];

• c. int[5] intArray = new int[];

• d. int[] intArray = new int[5];

Answer: d. int[] intArray = new int[5];

Question 5: Which of the following statements are correct for C# language?

• a. Every derived class does not define its own version of the virtual method.

• b. By default, the access mode for all methods in C# is virtual.

• c. If a derived class does not define its own version of the virtual method, then the one present in
the base class gets used.

• d. All of the above.

Answer: c. If a derived class does not define its own version of the virtual method, then the one present in
the base class gets used.

Question 6: Does C# support multiple inheritance?

• a. Yes

• b. No

Answer: b. No

Question 7: What is the purpose of the 'virtual' keyword?

• a. Creates abstract methods

• b. Enables method overriding

• c. Makes methods static

• d. Prevents inheritance

Answer: b. Enables method overriding


Question 8: 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 (//)

Question 9: 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);

• a. Error

• b. 100 101 102 ... Infinite

• c. 101

• d. 100

Answer: a. Error

Question 10: 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 {}


Question 11: Method body is enclosed in

• a. { }

• b. 0

• c. Begin End

• d. []

Answer: a. { }

Question 12: 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);

• a. 20, 20

• b. 10, 10

• c. 20, 10

Answer: c. 20, 10

Question 13: What is the purpose of the 'base' keyword in C#?

• a. To create a new base class

• b. To access members of the base class

• c. To define a static method


• d. To create an interface

Answer: b. To access members of the base class

Question 14: Is overriding of a function possible in the same class in C#?

• ○ a. Yes

• ○ b. No

Answer: b. No

Question 15: What does the 'abstract' keyword mean when applied to a class in C#?

• a. The class can be instantiated directly

• b. The class cannot be instantiated and may contain abstract methods

• c. The class is sealed and cannot be inherited

• d. The class can only have static methods

Answer: b. The class cannot be instantiated and may contain abstract methods

Question 16: 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

Question 17: Which of the following keywords enables modifying the data and behavior of a base class
by replacing its member with a new derived member?

• a. overloads

• b. overrides

• c. new

• d. base

Answer: c. new
Question 18: 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");

• a. True

• b. False

Answer: b. False

Question 19: In C#, multi-line comments are placed within ___.

• a. // and //

• b. \and //

• c. <!-- and -->

• d. /* and */

Answer: d. /* and */

Question 20: Which of the following is/are not types of arrays in C#?

• a. Single-dimensional

• b. Multidimensional

• c. Jagged arrays
• d. jazzed arrays

Answer: d. jazzed arrays

Question 21: How does a struct differ from a class in C#?

• a. Structs cannot have methods

• b. Structs are value types, classes are reference types

• c. Structs cannot inherit

• d. There is no difference

Answer: b. Structs are value types, classes are reference types

Question 22: Which keyword is used to prevent a class from being inherited?

• a. final

• b. static

• c. sealed

• d. private

Answer: c. sealed

Question 23: What is the purpose of the enum keyword in C#?

• a. To define a set of named constants

• b. To compare two variables

• c. To perform logical operations

• d. To define a loop construct

Answer: a. To define a set of named constants

Question 24: The type 'float' can be safely converted to____

• a. Double

• b. long

• c. decimal

• d. ufloat
Answer: a. Double

Question 25: 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

Question 26: Which keyword is used to specify that a method can be overridden in a derived class in C#?

• a. virtual

• b. override

• c. new

• d. base

Answer: a. virtual

Question 27: What is the purpose of the this keyword in C#?

• a. To refer to the current instance of a class

• b. To create an instance of a class

• c. To define a loop construct

• d. To compare two variables

Answer: a. To refer to the current instance of a class


Question 28: What will be the output of the following C# code?

public class emp {

public static int age = 40;

public static int salary = 25000;

public class record : emp {

new public static int salary = 50000;

static void Main(string[] args) {

Console.WriteLine(emp.age + " " + emp.salary + " " + salary);

• a. 40, 25000, 50000

• b. 40, 25000, 25000

• c. 40, 50000, 50000

• d. Error

Answer: a. 40, 25000, 50000

Question 29: Which C# concept has the capability of an object to take a number of different forms and
hence display behavior accordingly?

• a. Polymorphism

• b. Encapsulation

• c. Abstraction

• d. None of the above

Answer: a. Polymorphism

Question 30: Find the correctly initialized constructor?

• a. obj1 = new obj();

• b. obj1 = new obj;

• c. obj1 = new object();


• d. All of them are correct ways

Answer: a. obj1 = new obj();

Question 31: Which of the following String methods is used to compare two strings with each other?

• a. CompareTo()

• b. ConCat()

• c. Compare()

• d. Copy()

Answer: a. CompareTo()

Question 32: Which of the following is NOT a valid C# conditional operator?

• a. >

• b. !=

• c. ==

• d. <>

Answer: d. <>

Question 33: When a class serves as a base class for many derived classes, the situation is called______.

• a. Polymorphism

• b. hierarchical inheritance

• c. hybrid inheritance

• d. multipath inheritance

Answer: b. hierarchical inheritance

Question 34: What is the purpose of the interface keyword in C#?

• a. To define a contract for implementing classes

• b. To perform arithmetic operations

• c. To define a loop construct

• d. To compare two variables


Answer: a. To define a contract for implementing classes

Question 35: Choose the correct output for the following piece of code:

static void Main() {

int value = 100.ToString() == "100" ? 1 : -1;

Console.WriteLine(value);

• a. 1

• b. -1

• c. 0

• d. Error

Answer: a. 1

Question 36: Type of Conversion in which the compiler is unable to convert the data type implicitly is

• a. ushort to long

• b. int to uint

• c. All is correct

• d. byte to decimal

Answer: b. int to uint

Question 37: What does the 'virtual' keyword indicate in a method declaration in C#?

• a. The method cannot be overridden

• b. The method can be overridden in derived classes

• c. The method is static

• d. The method is private

Answer: b. The method can be overridden in derived classes


Question 38: 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;

Question 39: Which of the following is NOT a valid C# control flow statement?

• a. if-else

• b. switch

• c. for

• d. while-else

Answer: d. while-else

Question 40: What is the default access modifier for a class member in C#?

• a. public

• b. private

• c. protected

• d. internal

Answer: b. private

Question 41: Which data type is used to store text value in C#?

• a. text

• b. txt

• c. string

• d. str

Answer: c. string
Question 42: 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 []

Question 43: What is 'Console' in C#?

• a. Class

• b. Object

• c. Method

• d. Structure

Answer: a. Class

Question 44: In C#, a namespace is the collection of classes?

a) True
b) False

Answer: a.True

Question 45: 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


Question 46: Which of the following is the valid size of float data type?

a) 4 Bytes

b) 5 Bytes

c) 8 Bytes

Answer: a. 4Bytes

Question 47: What is the purpose of the this keyword in C#?

a) To refer to the current instance of a class

b) To create an instance of a class

c) To define a loop construct

d) To compare two variables

Answer: a. To refer to the current instance of a class

Question 48: How many times can a constructor be called during lifetime of the object?

a) Only once.

b) As many times as we call it.

c) Any number of times before the object is deleted.

d) None of the above

Answer: a. Only once

Question 49: Which of the following String method is used to compare two strings with each other?

a) Compare To()

b) ConCat()

c) Compare()

d) Copy()

Answer: a.Compare To()


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

Question 51: 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy