Unit 2: C# Programming Fundamentals - Practice Quiz

CSE253 — .Net Programming 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which C# data type is commonly used to store a whole number?

Variables and Data Types Easy
A. double
B. bool
C. int
D. char

2 Which symbol is used to assign a value to a variable in C#?

Variables and Data Types Easy
A. =
B. !=
C. +=
D. ==

3 Which method displays text on the console and moves to the next line?

Input and Output Operations Easy
A. Console.Write()
B. Console.Read()
C. Console.ReadLine()
D. Console.WriteLine()

4 Which method is used to read a complete line of input from the console?

Input and Output Operations Easy
A. Console.WriteLine()
B. Console.Read()
C. Console.ReadLine()
D. Console.Write()

5 Which statement prints Hello, World! in a C# console program?

C# Hello World Program Easy
A. Console.Print("Hello, World!");
B. Console.Input("Hello, World!");
C. Console.Write("Hello, World!");
D. Console.ReadLine("Hello, World!");

6 Which statement is used to execute code when a condition is true?

Conditional Statements Easy
A. using
B. class
C. loop
D. if

7 Which keyword provides an alternative block when an if condition is false?

Conditional Statements Easy
A. continue
B. default
C. case
D. else

8 Which loop is commonly used when the number of repetitions is known?

Loops Easy
A. for loop
B. do-while loop
C. switch loop
D. while loop

9 Which loop checks its condition before executing its body?

Loops Easy
A. while loop
B. switch loop
C. foreach loop
D. do-while loop

10 Which jump statement immediately exits a loop?

Jump Statements Easy
A. continue
B. break
C. return
D. goto

11 Which statement skips the remaining statements in the current loop iteration?

Jump Statements Easy
A. throw
B. continue
C. break
D. return

12 What is the index of the first element in a C# array?

Arrays Easy
A. 0
B. 2
C. -1
D. 1

13 Which property returns the number of elements in a C# array?

Arrays Easy
A. Capacity
B. Length
C. Size
D. Count

14 Which method converts all letters in a string to uppercase?

Strings and String Methods Easy
A. ToUpper()
B. ConvertUpper()
C. ToLower()
D. UpperCase()

15 Which property returns the number of characters in a string?

Strings and String Methods Easy
A. Length
B. Characters
C. Count
D. Size

16 Which collection stores elements as key-value pairs?

Collections in C# Easy
A. Queue<T>
B. Dictionary<TKey, TValue>
C. ArrayList
D. List<T>

17 What is a method in C#?

Methods and Parameter Passing Easy
A. A named block of code
B. A data storage location
C. A compiler setting
D. A program comment

18 What is an object in C#?

Classes and Objects Easy
A. An instance of a class
B. A compiler error
C. A program namespace
D. A type of loop

19 When is a constructor usually called?

Constructors Easy
A. When a loop ends
B. When a method is hidden
C. When an object is created
D. When a file is deleted

20 Which access modifier allows a member to be accessed from any code that can see the class?

Access Modifiers Easy
A. private
B. protected
C. internal
D. public

21 What is the value of result after this code executes?

CSHARP
int count = 5;
double result = count / 2;

Variables and Data Types Medium
A. 2.0
B. Compilation error
C. 3.0
D. 2.5

22 Which statement correctly reads an integer entered by the user and stores it in age?

Input and Output Operations Medium
A. int age = Console.Read();
B. int age = Console.ReadLine();
C. int age = Convert.ToInt32(Console.ReadLine());
D. int age = Parse(Console.ReadLine());

23 Which statement is required to display Hello, World! in a standard C# console program?

C# Hello World Program Medium
A. System.Print("Hello, World!");
B. Print.Console("Hello, World!");
C. Console.Display("Hello, World!");
D. Console.WriteLine("Hello, World!");

24 What is printed by the following code?

CSHARP
int marks = 72;
if (marks >= 80)
    Console.Write("A");
else if (marks >= 60)
    Console.Write("B");
else
    Console.Write("C");

Conditional Statements Medium
A. C
B. No output
C. A
D. B

25 How many times is Console.Write(i); executed?

CSHARP
for (int i = 1; i <= 10; i += 2)
{
    Console.Write(i);
}

Loops Medium
A. 6 times
B. 4 times
C. 10 times
D. 5 times

26 What is printed by this code?

CSHARP
for (int i = 1; i <= 5; i++)
{
    if (i == 3)
        continue;
    Console.Write(i);
}

Jump Statements Medium
A. 345
B. 12
C. 12345
D. 1245

27 What is the value of numbers[2] after this code executes?

CSHARP
int[] numbers = { 4, 8, 12, 16 };
numbers[2] = numbers[0] + numbers[1];

Arrays Medium
A. 20
B. 24
C. 12
D. 16

28 What is the value of text after this code executes?

CSHARP
string text = "  CSharp  ";
text = text.Trim().ToUpper();

Strings and String Methods Medium
A. " CSHARP "
B. "CSHARP"
C. "csharp"
D. " CSharp "

29 Which collection is most appropriate when items must be accessed by a unique string key?

Collections in C# Medium
A. Dictionary<string, int>
B. List<int>
C. Stack<double>
D. Queue<string>

30 What is printed by the following code?

CSHARP
static void Update(int x)
{
    x = 20;
}

int value = 10;
Update(value);
Console.Write(value);

Methods and Parameter Passing Medium
A. 10
B. Compilation error
C. 20
D. 0

31 Which code correctly creates an object from the following class?

CSHARP
class Student
{
    public string Name;
}

Classes and Objects Medium
A. object s = create Student();
B. Student s = new Student();
C. Student s = Student();
D. Student s = new Student;

32 What is the purpose of the constructor in this class?

CSHARP
class Product
{
    public string Code;

    public Product(string code)
    {
        Code = code;
    }
}

Constructors Medium
A. It destroys the object
B. It converts the object to a string
C. It prevents object creation
D. It initializes the object's data

33 A field should be accessible only inside its own class. Which access modifier provides this restriction?

Access Modifiers Medium
A. public
B. protected
C. internal
D. private

34 Which design best demonstrates encapsulation for a bank account balance?

Object-Oriented Programming Concepts - Encapsulation Medium
A. Remove balance and calculate it when needed
B. Make balance private and update it through methods
C. Store balance in a global variable
D. Make balance public and change it anywhere

35 Which situation best illustrates abstraction?

Object-Oriented Programming Concepts - Abstraction Medium
A. Exposing every internal calculation to the user
B. Using an ATM without knowing its internal processing
C. Allowing every class to modify shared data
D. Copying all fields from one object to another

36 Given class Dog : Animal, which statement is correct?

Object-Oriented Programming Concepts - Inheritance Medium
A. Animal inherits from Dog
B. Dog can inherit accessible members from Animal
C. Dog cannot define its own methods
D. Animal must be declared as a structure

37 What concept is demonstrated when a derived class provides its own implementation of a base class method declared with virtual?

Object-Oriented Programming Concepts - Polymorphism Medium
A. Encapsulation
B. Constructor chaining
C. Method overloading
D. Method overriding

38 Which block executes whether or not an exception occurs?

Exception Handling Medium
A. finally
B. throw
C. try
D. catch

39 Which statement correctly reads all text from a file named data.txt?

File Handling and Debugging Fundamentals Medium
A. File.OpenTextAll("data.txt")
B. File.ReadAllText("data.txt")
C. File.GetAllText("data.txt")
D. File.ReadText("data.txt")

40 A program produces the wrong result but runs without crashing. Which debugging action is most useful for locating the cause?

File Handling and Debugging Fundamentals Medium
A. Rename all variables immediately
B. Delete the exception handlers
C. Increase the console window size
D. Add a breakpoint and inspect variable values

41 What does the following C# code print?

byte b = 250;
try
{
checked
{
b += 10;
}
Console.Write($"V{b}");
}
catch (OverflowException)
{
Console.Write($"E{b}");
}

Variables and Data Types Hard
A. E4
B. V4
C. E250
D. V260

42 Standard input contains exactly one empty line followed by end-of-file. What does this code print?

string? a = Console.ReadLine();
string? b = Console.ReadLine();
Console.Write($"{a?.Length ?? -1},{b?.Length ?? -1}");

Input and Output Operations Hard
A. 1,0
B. -1,-1
C. 0,0
D. 0,-1

43 In a C# 12 console project, what is written to standard output by this program?

Console.Write("T");

class Program
{
public static void Main() => Console.Write("M");
}

C# Hello World Program Hard
A. TM
B. Nothing; compilation fails
C. Only T
D. Only M

44 What value is printed?

int x = 0;
bool A() { x += 1; return false; }
bool B() { x *= 10; return true; }

if (A() && B() || B())
x += 3;

Console.Write(x);

Conditional Statements Hard
A. 103
B. 3
C. 13
D. 10

45 What value does the following loop print?

int sum = 0;
for (int i = 0; i < 4; i++)
{
switch (i)
{
case 1: continue;
case 2: break;
}
sum += i;
}
Console.Write(sum);

Loops Hard
A. 5
B. 6
C. 2
D. 3

46 What does this program print?

static int F()
{
int x = 1;
try
{
return x;
}
finally
{
x = 2;
Console.Write(x);
}
}

Console.Write(F());

Jump Statements Hard
A. 22
B. 11
C. 21
D. 12

47 What does the following code print?

object[] values = new string[2];
values[0] = "ok";

try
{
values[1] = new object();
Console.Write("A");
}
catch (ArrayTypeMismatchException)
{
Console.Write("B");
}

Console.Write(values[0]);

Arrays Hard
A. Bok
B. Aok
C. BObject
D. AObject

48 What does this code print?

string s = "A\U0001F600B";
string t = s.Substring(1, 2);
Console.Write($"{s.Length}:{t.Length}:{char.IsSurrogatePair(t, 0)}");

Strings and String Methods Hard
A. 3:1:True
B. 3:2:False
C. 4:2:True
D. 4:1:False

49 What does the following code print?

var values = new List<int> { 1, 2, 3 };
try
{
foreach (int value in values)
{
if (value == 2)
values.Remove(value);
Console.Write(value);
}
}
catch (InvalidOperationException)
{
Console.Write("E");
}

Collections in C# Hard
A. 123E
B. 1E
C. 123
D. 12E

50 What values are printed?

static void Update(int a, ref int b, out int c)
{
a++;
b += a;
c = a + b;
}

int x = 2;
Update(x, ref x, out int y);
Console.Write($"{x},{y}");

Methods and Parameter Passing Hard
A. 2,7
B. 5,8
C. 5,7
D. 3,6

51 What does this object-initializer code print?

class Counter
{
private int x;
public int X
{
get => x;
set { x = value; Y = x + 1; }
}
public int Y { get; set; }
}

var c = new Counter { Y = 10, X = 3 };
Console.Write($"{c.X},{c.Y}");

Classes and Objects Hard
A. 4,10
B. 3,4
C. 3,10
D. 4,11

52 What is the output when new C() executes?

class C
{
public C() : this(1) { Console.Write("A"); }
private C(int n) : this("x") { Console.Write("B"); }
private C(string s) { Console.Write("C"); }
}

Constructors Hard
A. CAB
B. BCA
C. ABC
D. CBA

53 Assembly A defines the following class:

public class Base
{
private protected int P;
protected internal int Q;
}

Assembly B, which has no friend-assembly access to A, defines class Derived : Base. Inside an instance method of Derived, variables Base b and Derived d are available. Which statement compiles?

Access Modifiers Hard
A. this.P = 1;
B. b.Q = 1;
C. b.P = 1;
D. d.Q = 1;

54 A class stores data in private readonly List<int> _items. A view obtained by a caller must reflect later internal additions, but callers must not be able to mutate the list through that view. Which implementation best satisfies both requirements?

Object-Oriented Programming Concepts - Encapsulation Hard
A. Return a cached _items.AsReadOnly() wrapper.
B. Return _items as an IReadOnlyList<int>.
C. Return a cached _items.ToArray() snapshot.
D. Return a new _items.ToList() copy.

55 Consider this C# 12 code:

interface I
{
void M() => Console.Write("I");
}

abstract class A : I { }
class B : A { }

Which statement correctly describes the two expressions ((I)new B()).M() and new B().M()?

Object-Oriented Programming Concepts - Abstraction Hard
A. Both compile and print I.
B. Neither compiles because B does not implement M.
C. The first prints I; the second does not compile.
D. The first does not compile; the second prints I.

56 What does this program print?

class Base
{
public string Name() => "B";
}

class Derived : Base
{
public new string Name() => "D";
}

Derived d = new Derived();
Base b = d;
Console.Write(d.Name() + b.Name());

Object-Oriented Programming Concepts - Inheritance Hard
A. DD
B. DB
C. BB
D. BD

57 What does the following code print?

class Base
{
public virtual string F(object value) => "BO";
}

class Derived : Base
{
public override string F(object value) => "DO";
public string F(string value) => "DS";
}

Base x = new Derived();
Console.Write(x.F("text"));

Object-Oriented Programming Concepts - Polymorphism Hard
A. DS
B. DO
C. BO
D. An ambiguity error

58 What value is printed?

int x = 0;
try
{
throw new InvalidOperationException();
}
catch (Exception) when (++x == 0)
{
x += 100;
}
catch (InvalidOperationException)
{
x += 10;
}
finally
{
x *= 2;
}
Console.Write(x);

Exception Handling Hard
A. 20
B. 202
C. 22
D. 2

59 Assume path is writable and initially unused. What content is printed?

File.WriteAllText(path, "abcdef");
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Write))
{
stream.WriteByte((byte)'X');
}
Console.Write(File.ReadAllText(path));

File Handling and Debugging Fundamentals Hard
A. abcdefX
B. Xbcdef
C. An IOException is thrown
D. X

60 A debugger must retain the original exception throw site after the exception is logged in a catch block. Which replacement for RETHROW satisfies that requirement?

catch (Exception ex)
{
Log(ex);
RETHROW
}

File Handling and Debugging Fundamentals Hard
A. throw new Exception(ex.Message);
B. throw new Exception("failed", ex);
C. throw;
D. throw ex;