Unit 2: C# Programming Fundamentals - Practice Quiz

INT402 — Modern Web Programming Tools And Techniques 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. bool
B. int
C. double
D. string

2 Which statement displays Hello World in a C# console application?

C# Hello World Program Easy
A. System.Print("Hello World");
B. Console.ReadLine("Hello World");
C. Console.GetLine("Hello World");
D. Console.WriteLine("Hello World");

3 What is the value of 2 + 3 * 4 in C#?

Operators and Operator Precedence Easy
A. 11
B. 14
C. 24
D. 20

4 Which C# statement executes code only when a specified condition is true?

Conditional Statements Easy
A. for
B. break
C. return
D. if

5 Which loop always executes its body at least once?

Loops: For, While and Do-While Easy
A. foreach loop
B. do-while loop
C. for loop
D. while loop

6 Which jump statement immediately exits the nearest loop?

Jump Statements: Break, Continue, Return and Goto Easy
A. continue
B. goto
C. return
D. break

7 Which declaration creates a one-dimensional integer array with five elements?

Arrays and Types of Arrays Easy
A. int values = new int[5];
B. array<int> values = new[5];
C. int[] values = new int[5];
D. int[5] values = new int;

8 Which expression returns the number of elements in an array named scores?

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

9 Which string method converts "hello" to "HELLO"?

Strings and String Methods Easy
A. Trim()
B. ToUpper()
C. ToLower()
D. Replace()

10 Which operator can join two strings in C#?

Manipulating Strings Easy
A. *
B. -
C. +
D. /

11 Which object-oriented concept allows one method name to have different behaviors?

Object-Oriented Programming Concepts Easy
A. Polymorphism
B. Inheritance
C. Abstraction
D. Encapsulation

12 What is an object in C#?

Classes and Objects Easy
A. A block for handling errors
B. A type of loop statement
C. An instance of a class
D. A name for a namespace

13 Which access modifier restricts a class member so it can be accessed only within the same class?

Encapsulation and Abstraction Easy
A. public
B. private
C. protected
D. internal

14 When is a constructor normally called in C#?

Constructors Easy
A. When an exception is caught
B. When an object is created
C. When a loop is completed
D. When a method returns

15 Which symbol is used to specify a base class in a C# class declaration?

Inheritance Easy
A. ::
B. :
C. ;
D. =>

16 Which keyword allows a base-class method to be replaced with a derived-class implementation?

Polymorphism Easy
A. private
B. static
C. virtual
D. sealed

17 Which statement about an abstract class in C# is correct?

Abstract Classes Easy
A. It cannot contain any methods
B. It cannot define constructors
C. It cannot be instantiated directly
D. It cannot have derived classes

18 Which block contains code that might cause an exception?

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

19 What does the continue statement do inside a loop?

Jump Statements: Break, Continue, Return and Goto Easy
A. Moves to a named label
B. Exits the current method
C. Starts the next iteration
D. Ends the entire loop

20 Which block is used to handle an exception thrown by code in a try block?

Exception Handling Easy
A. catch
B. using
C. finally
D. checked

21 What happens when the following C# code is compiled?

var quantity = 10;
quantity = 10.5;

Variables and Data Types Medium
A. It compiles and rounds the value to 10.
B. It fails because quantity was inferred as int.
C. It fails because var cannot store numeric values.
D. It compiles and stores 10.5 as a double.

22 What is the output of the following program?

class Program
{
static void Main()
{
Console.Write("Hello");
Console.WriteLine(" World");
Console.Write("C#");
}
}

C# Hello World Program Medium
A. Hello World
C#
B. Hello World C#
C. Hello
World
C#
D. Hello
World C#

23 What value is printed by the following code?

int a = 5, b = 2;
int result = a++ * --b + a;
Console.WriteLine(result);

Operators and Operator Precedence Medium
A. 13
B. 12
C. 11
D. 10

24 What is printed by this nested conditional statement?

int x = 12;
if (x % 2 == 0)
if (x > 10)
Console.Write("A");
else
Console.Write("B");
else
Console.Write("C");

Conditional Statements Medium
A. AC
B. C
C. A
D. B

25 What value is printed after this loop finishes?

int sum = 0;
for (int i = 1; i <= 5; i++)
{
if (i % 2 == 0) continue;
sum += i;
}
Console.WriteLine(sum);

Loops: For, While and Do-While Medium
A. 9
B. 15
C. 6
D. 12

26 What does the following loop print?

for (int i = 0; i < 7; i++)
{
if (i == 2) continue;
if (i == 5) break;
Console.Write(i);
}

Jump Statements: Break, Continue, Return and Goto Medium
A. 0134
B. 01234
C. 012345
D. 013456

27 What is printed by the following jagged-array code?

int[][] values =
{
new int[] { 1, 2 },
new int[] { 3, 4, 5 }
};
Console.WriteLine(values[1][2]);

Arrays and Types of Arrays Medium
A. 5
B. 4
C. 3
D. 2

28 What value is printed after the array is sorted and reversed?

int[] numbers = { 7, 2, 9, 4 };
Array.Sort(numbers);
Array.Reverse(numbers);
Console.WriteLine(numbers[1]);

Manipulating Arrays Medium
A. 4
B. 2
C. 9
D. 7

29 What is the output of the following string operation?

string text = "Modern Web";
Console.WriteLine(text.Substring(7, 3).ToUpper());

Strings and String Methods Medium
A. WEB
B. ERN
C. Web
D. MOD

30 What is printed by this code?

string language = "C#";
language.Replace("#", "Sharp");
Console.WriteLine(language);

Manipulating Strings Medium
A. C Sharp
B. Sharp
C. C#
D. CSharp

31 An application defines an INotification interface with a Send() method. EmailNotification and SmsNotification provide different implementations of Send(). Which OOP concept is primarily demonstrated when both objects are used through INotification references?

Object-Oriented Programming Concepts Medium
A. Polymorphism
B. Encapsulation
C. Composition
D. Inheritance

32 Given the class Student with a public integer field Mark, what is printed?

Student first = new Student();
first.Mark = 70;
Student second = first;
second.Mark = 85;
Console.WriteLine(first.Mark);

Classes and Objects Medium
A. 75
B. 85
C. 0
D. 70

33 Which design best encapsulates a bank account balance while preventing negative deposits?

Encapsulation and Abstraction Medium
A. Use a public Balance field and modify it directly.
B. Use a protected field and access it from every class.
C. Use a static field and update it through each object.
D. Use a private field and validate values in a public method.

34 What is printed when the following object is created?

class Base
{
public Base() { Console.Write("Base "); }
}
class Derived : Base
{
public Derived() { Console.Write("Derived"); }
}

Derived item = new Derived();

Constructors Medium
A. Derived Derived
B. Base Base
C. Base Derived
D. Derived Base

35 A base class declares protected int code = 10;. Which statement about code is correct?

Inheritance Medium
A. It is accessible from every class in the assembly.
B. It is accessible only inside the base class.
C. It is accessible inside the base and derived classes.
D. It is accessible only through a base-class object.

36 What is printed by the following code?

class Animal
{
public virtual void Speak() { Console.Write("Animal"); }
}
class Dog : Animal
{
public override void Speak() { Console.Write("Dog"); }
}

Animal pet = new Dog();
pet.Speak();

Polymorphism Medium
A. Dog
B. AnimalDog
C. Animal
D. A compile-time error

37 Consider the following declaration:

abstract class Shape
{
public abstract double Area();
}

What must a non-abstract class derived from Shape do?

Abstract Classes Medium
A. Hide Area using the new keyword.
B. Call Area from its constructor.
C. Declare another abstract Area method.
D. Override Area with an implementation.

38 What is printed by the following code?

try
{
int value = int.Parse("abc");
Console.Write("Parsed ");
}
catch (FormatException)
{
Console.Write("Invalid ");
}
finally
{
Console.Write("Done");
}

Exception Handling Medium
A. Invalid Parsed
B. Done Invalid
C. Invalid Done
D. Parsed Done

39 What value is printed by the following expression?

int result = 8 >> 1 + 1;
Console.WriteLine(result);

Operators and Operator Precedence Medium
A. 8
B. 2
C. 16
D. 4

40 What does the following program print?

static int GetValue()
{
int value = 1;
try
{
return value;
}
finally
{
value = 2;
}
}

Console.WriteLine(GetValue());

Exception Handling Medium
A. 2
B. 0
C. 1
D. A runtime exception

41 What does the following C# code print?

CSHARP
byte value = 250;
var result = value + 10;
Console.Write($"{result}:{result.GetType().Name}");

Variables and Data Types Hard
A. 4:Byte
B. 260:Byte
C. 260:Int32
D. An OverflowException is thrown

42 A top-level C# program contains the following code and is run without command-line arguments. What are its console output and process exit code?

CSHARP
Console.Write($"{args.Length}:{Environment.ExitCode}");
return 7;

C# Hello World Program Hard
A. Output 0:0; exit code 0
B. Output 0:7; exit code 0
C. Output 0:0; exit code 7
D. Output 0:7; exit code 7

43 What value is assigned to result?

CSHARP
int result = 1 + 2 * 3 << 1 & 15;

Operators and Operator Precedence Hard
A. 15
B. 7
C. 6
D. 14

44 What does this code print?

CSHARP
int x = 0;

if (++x == 1 || ++x == 2 && ++x == 3)
{
    x += 10;
}

Console.Write(x);

Conditional Statements Hard
A. 13
B. 3
C. 1
D. 11

45 What does the following loop print?

CSHARP
int i = 0;
int sum = 0;

do
{
    i++;
    if (i % 2 == 0)
        continue;

    sum += i;
}
while (i < 4);

Console.Write(sum);

Loops: For, While and Do-While Hard
A. 10
B. 3
C. 6
D. 4

46 What is printed when F() is called?

CSHARP
static int F()
{
    try
    {
        goto Done;
    }
    finally
    {
        Console.Write("F");
    }

Done:
    Console.Write("D");
    return 1;
}

Console.Write(F());

Jump Statements: Break, Continue, Return and Goto Hard
A. F1D
B. FD1
C. D1F
D. DF1

47 What happens when this code is executed?

CSHARP
object[] values = new string[2];
values[0] = "valid";
values[1] = 42;
Console.Write(string.Join(",", values));

Arrays and Types of Arrays Hard
A. It prints valid,System.Int32
B. It prints valid,42
C. It throws ArrayTypeMismatchException
D. It throws InvalidCastException

48 What does this code print?

CSHARP
int[,] rectangular = new int[2, 3];
int[][] jagged =
{
    new int[1],
    new int[4]
};

Console.Write($"{rectangular.Length},{rectangular.GetLength(1)},{jagged.Length},{jagged[1].Length}");

Arrays and Types of Arrays Hard
A. 2,3,2,4
B. 6,3,2,4
C. 6,3,5,4
D. 6,2,2,4

49 What is the final content of numbers?

CSHARP
int[] numbers = { 1, 2, 3, 4, 5 };
Array.Copy(numbers, 0, numbers, 1, 4);

Manipulating Arrays Hard
A. { 1, 2, 2, 3, 4 }
B. { 2, 3, 4, 5, 5 }
C. { 1, 1, 2, 3, 4 }
D. { 1, 1, 1, 1, 1 }

50 What does this code print on a modern .NET runtime?

CSHARP
string input = "  a ,, b,  ";
string[] parts = input.Split(
    ',',
    StringSplitOptions.TrimEntries |
    StringSplitOptions.RemoveEmptyEntries);

Console.Write($"{parts.Length}:{string.Join("|", parts)}");

Strings and String Methods Hard
A. 3:a||b
B. 4:a||b|
C. 2:a|b
D. 2: a | b

51 What does the following code print?

CSHARP
string first = "ab" + "cd";
string second = new string(new[] { 'a', 'b', 'c', 'd' });

Console.Write($"{first == second}:{ReferenceEquals(first, second)}");

Manipulating Strings Hard
A. True:False
B. True:True
C. False:True
D. False:False

52 What does this code print?

CSHARP
interface ILeft
{
    string Name();
}

interface IRight
{
    string Name();
}

class Both : ILeft, IRight
{
    string ILeft.Name() => "L";
    string IRight.Name() => "R";
    public string Name() => "P";
}

var item = new Both();
ILeft left = item;
Console.Write(item.Name() + left.Name() + ((IRight)item).Name());

Object-Oriented Programming Concepts Hard
A. PLR
B. PRR
C. PLL
D. PPP

53 What does this code print?

CSHARP
class Box
{
    public int Value;
}

static void Update(Box box)
{
    box.Value = 2;
    box = new Box { Value = 3 };
}

var original = new Box { Value = 1 };
Update(original);
Console.Write(original.Value);

Classes and Objects Hard
A. A null-reference exception
B. 2
C. 1
D. 3

54 Which property declaration allows assignment only from the containing class or from derived classes located in the same assembly, while allowing public reads?

Encapsulation and Abstraction Hard
A. public int Value { get; private protected set; }
B. public int Value { get; internal set; }
C. public int Value { get; protected set; }
D. public int Value { get; protected internal set; }

55 What is the output when new Derived() is executed?

CSHARP
class Base
{
    protected static int Mark(string text)
    {
        Console.Write(text);
        return 0;
    }

    private int b = Mark("Bf ");

    public Base()
    {
        Console.Write("Bc ");
    }
}

class Derived : Base
{
    private int d = Mark("Df ");

    public Derived()
    {
        Console.Write("Dc");
    }
}

Constructors Hard
A. Df Bf Bc Dc
B. Bf Bc Df Dc
C. Bf Df Dc Bc
D. Df Dc Bf Bc

56 What does this code print?

CSHARP
class Base
{
    public string Show() => "Base";
}

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

Base item = new Derived();
Console.Write(item.Show());

Inheritance Hard
A. A compile-time error
B. Derived
C. Base
D. BaseDerived

57 What does this code print?

CSHARP
class Base
{
    public virtual string M(object value) => "Base-object";
}

class Derived : Base
{
    public override string M(object value) => "Derived-object";
    public string M(string value) => "Derived-string";
}

Base item = new Derived();
Console.Write(item.M("text"));

Polymorphism Hard
A. Derived-string
B. Base-string
C. Derived-object
D. Base-object

58 What does this code print?

CSHARP
class A
{
    public virtual string Describe() => "A";
}

abstract class B : A
{
    public abstract override string Describe();
}

class C : B
{
    public override string Describe() => "C";
}

A value = new C();
Console.Write(value.Describe());

Abstract Classes Hard
A. A
B. A compile-time error
C. C
D. B

59 What does the following code print?

CSHARP
static bool Filter()
{
    Console.Write("L");
    return false;
}

try
{
    try
    {
        throw new InvalidOperationException();
    }
    catch (Exception) when (Filter())
    {
        Console.Write("C");
    }
    finally
    {
        Console.Write("F");
    }
}
catch (Exception)
{
    Console.Write("O");
}

Exception Handling Hard
A. LFO
B. LFCO
C. LCF
D. FLO

60 What does this code print?

CSHARP
static int Calculate()
{
    int value = 1;

    try
    {
        return value;
    }
    finally
    {
        value = 2;
        Console.Write(value);
    }
}

Console.Write(Calculate());

Exception Handling Hard
A. 21
B. 22
C. 11
D. 12