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

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.ReadLine()
C. Console.Read()
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.Write()
B. Console.ReadLine()
C. Console.Read()
D. Console.WriteLine()

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

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

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

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

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

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

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

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

9 Which loop checks its condition before executing its body?

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

10 Which jump statement immediately exits a loop?

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

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

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

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

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

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

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

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

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

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

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

16 Which collection stores elements as key-value pairs?

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

17 What is a method in C#?

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

18 What is an object in C#?

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

19 When is a constructor usually called?

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

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

Access Modifiers Easy
A. private
B. internal
C. protected
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. Compilation error
B. 3.0
C. 2.5
D. 2.0

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

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

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

C# Hello World Program Medium
A. Console.WriteLine("Hello, World!");
B. System.Print("Hello, World!");
C. Print.Console("Hello, World!");
D. Console.Display("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. B
B. A
C. No output
D. C

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

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

Loops Medium
A. 10 times
B. 4 times
C. 5 times
D. 6 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. 1245
B. 12
C. 12345
D. 345

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. 16
B. 12
C. 20
D. 24

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. Stack<double>
C. Queue<string>
D. List<int>

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 prevents object creation
B. It converts the object to a string
C. It destroys the object
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. private
B. public
C. protected
D. internal

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

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

35 Which situation best illustrates abstraction?

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

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

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

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. Constructor chaining
B. Encapsulation
C. Method overloading
D. Method overriding

38 Which block executes whether or not an exception occurs?

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

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. Increase the console window size
B. Rename all variables immediately
C. Add a breakpoint and inspect variable values
D. Delete the exception handlers

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. V4
B. E4
C. V260
D. E250

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. Nothing; compilation fails
B. Only M
C. TM
D. Only T

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. 13
B. 3
C. 103
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. 2
C. 3
D. 6

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. 21
B. 11
C. 12
D. 22

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. 4:1:False
B. 3:2:False
C. 4:2:True
D. 3:1:True

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. 12E
C. 123
D. 1E

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. 5,7
B. 5,8
C. 3,6
D. 2,7

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,10
C. 4,11
D. 3,4

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. ABC
B. CBA
C. CAB
D. BCA

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 new _items.ToList() copy.
B. Return a cached _items.AsReadOnly() wrapper.
C. Return _items as an IReadOnlyList<int>.
D. Return a cached _items.ToArray() snapshot.

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. The first does not compile; the second prints I.
B. The first prints I; the second does not compile.
C. Neither compiles because B does not implement M.
D. Both compile and print 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. BD
C. DB
D. BB

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. An ambiguity error
B. DO
C. BO
D. DS

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. 22
B. 202
C. 2
D. 20

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. Xbcdef
B. abcdefX
C. X
D. An IOException is thrown

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 ex;
C. throw new Exception("failed", ex);
D. throw;