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

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

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

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

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

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

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

5 Which loop always executes its body at least once?

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

6 Which jump statement immediately exits the nearest loop?

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

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. int[] values = new int[5];
C. int[5] values = new int;
D. array<int> values = new[5];

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

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

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

Strings and String Methods Easy
A. ToUpper()
B. ToLower()
C. Trim()
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. Encapsulation
B. Polymorphism
C. Abstraction
D. Inheritance

12 What is an object in C#?

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

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

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

14 When is a constructor normally called in C#?

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

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. virtual
B. private
C. static
D. sealed

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

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

18 Which block contains code that might cause an exception?

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

19 What does the continue statement do inside a loop?

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

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

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

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

var quantity = 10;
quantity = 10.5;

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

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

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. 6
B. 9
C. 12
D. 15

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. 012345
B. 0134
C. 013456
D. 01234

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. 4
B. 2
C. 5
D. 3

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. 7
B. 4
C. 2
D. 9

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. Web
C. ERN
D. MOD

30 What is printed by this code?

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

Manipulating Strings Medium
A. C#
B. Sharp
C. C Sharp
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. Composition
B. Inheritance
C. Encapsulation
D. Polymorphism

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. 70
B. 0
C. 85
D. 75

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

Encapsulation and Abstraction Medium
A. Use a static field and update it through each object.
B. Use a protected field and access it from every class.
C. Use a public Balance field and modify it directly.
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. Base Derived
B. Derived Base
C. Base Base
D. Derived Derived

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

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

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. A compile-time error
B. Animal
C. Dog
D. AnimalDog

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. Declare another abstract Area method.
B. Override Area with an implementation.
C. Hide Area using the new keyword.
D. Call Area from its constructor.

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. 16
B. 8
C. 2
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. 1
C. 0
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 7
B. Output 0:0; exit code 0
C. Output 0:7; exit code 0
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. 14
D. 6

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. 1
B. 11
C. 3
D. 13

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. 4
B. 3
C. 10
D. 6

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. DF1
C. D1F
D. FD1

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 InvalidCastException
D. It throws ArrayTypeMismatchException

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. 6,2,2,4
B. 2,3,2,4
C. 6,3,5,4
D. 6,3,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, 1, 1, 1 }
D. { 1, 1, 2, 3, 4 }

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

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. PPP
B. PRR
C. PLR
D. PLL

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. 2
B. 1
C. 3
D. A null-reference exception

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; internal set; }
B. public int Value { get; private protected 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. Bf Df Dc Bc
B. Df Bf Bc Dc
C. Bf Bc Df Dc
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. Derived
B. A compile-time error
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. Base-object
B. Derived-string
C. Derived-object
D. Base-string

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 compile-time error
B. A
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. LFCO
B. LFO
C. FLO
D. LCF

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