Unit 2: C# Programming Fundamentals - Practice Quiz
1 Which C# data type is commonly used to store a whole number?
bool
int
double
string
2
Which statement displays Hello World in a C# console application?
System.Print("Hello World");
Console.ReadLine("Hello World");
Console.GetLine("Hello World");
Console.WriteLine("Hello World");
3
What is the value of 2 + 3 * 4 in C#?
11
14
24
20
4 Which C# statement executes code only when a specified condition is true?
for
break
return
if
5 Which loop always executes its body at least once?
foreach loop
do-while loop
for loop
while loop
6 Which jump statement immediately exits the nearest loop?
continue
goto
return
break
7 Which declaration creates a one-dimensional integer array with five elements?
int values = new int[5];
array<int> values = new[5];
int[] values = new int[5];
int[5] values = new int;
8
Which expression returns the number of elements in an array named scores?
scores.Capacity
scores.Length
scores.Count
scores.Size
9
Which string method converts "hello" to "HELLO"?
Trim()
ToUpper()
ToLower()
Replace()
10 Which operator can join two strings in C#?
*
-
+
/
11 Which object-oriented concept allows one method name to have different behaviors?
12 What is an object in C#?
13 Which access modifier restricts a class member so it can be accessed only within the same class?
public
private
protected
internal
14 When is a constructor normally called in C#?
15 Which symbol is used to specify a base class in a C# class declaration?
::
:
;
=>
16 Which keyword allows a base-class method to be replaced with a derived-class implementation?
private
static
virtual
sealed
17 Which statement about an abstract class in C# is correct?
18 Which block contains code that might cause an exception?
catch
try
finally
throw
19
What does the continue statement do inside a loop?
20
Which block is used to handle an exception thrown by code in a try block?
catch
using
finally
checked
21
What happens when the following C# code is compiled?
var quantity = 10;
quantity = 10.5;
10.
quantity was inferred as int.
var cannot store numeric values.
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#
World
C#
World C#
23
What value is printed by the following code?
int a = 5, b = 2;
int result = a++ * --b + a;
Console.WriteLine(result);
13
12
11
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");
AC
C
A
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);
9
15
6
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);
}
0134
01234
012345
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]);
5
4
3
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]);
4
2
9
7
29
What is the output of the following string operation?
string text = "Modern Web";
Console.WriteLine(text.Substring(7, 3).ToUpper());
WEB
ERN
Web
MOD
30
What is printed by this code?
string language = "C#";
language.Replace("#", "Sharp");
Console.WriteLine(language);
C Sharp
Sharp
C#
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?
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);
75
85
0
70
33 Which design best encapsulates a bank account balance while preventing negative deposits?
Balance field and modify it directly.
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();
Derived Derived
Base Base
Base Derived
Derived Base
35
A base class declares protected int code = 10;. Which statement about code is correct?
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();
Dog
AnimalDog
Animal
37
Consider the following declaration:
abstract class Shape
{
public abstract double Area();
}
What must a non-abstract class derived from Shape do?
Area using the new keyword.
Area from its constructor.
Area method.
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");
}
Invalid Parsed
Done Invalid
Invalid Done
Parsed Done
39
What value is printed by the following expression?
int result = 8 >> 1 + 1;
Console.WriteLine(result);
8
2
16
4
40
What does the following program print?
static int GetValue()
{
int value = 1;
try
{
return value;
}
finally
{
value = 2;
}
}
Console.WriteLine(GetValue());
2
0
1
41
What does the following C# code print?
byte value = 250;
var result = value + 10;
Console.Write($"{result}:{result.GetType().Name}");
4:Byte
260:Byte
260:Int32
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?
Console.Write($"{args.Length}:{Environment.ExitCode}");
return 7;
0:0; exit code 0
0:7; exit code 0
0:0; exit code 7
0:7; exit code 7
43
What value is assigned to result?
int result = 1 + 2 * 3 << 1 & 15;
15
7
6
14
44
What does this code print?
int x = 0;
if (++x == 1 || ++x == 2 && ++x == 3)
{
x += 10;
}
Console.Write(x);
13
3
1
11
45
What does the following loop print?
int i = 0;
int sum = 0;
do
{
i++;
if (i % 2 == 0)
continue;
sum += i;
}
while (i < 4);
Console.Write(sum);
10
3
6
4
46
What is printed when F() is called?
static int F()
{
try
{
goto Done;
}
finally
{
Console.Write("F");
}
Done:
Console.Write("D");
return 1;
}
Console.Write(F());
F1D
FD1
D1F
DF1
47
What happens when this code is executed?
object[] values = new string[2];
values[0] = "valid";
values[1] = 42;
Console.Write(string.Join(",", values));
valid,System.Int32
valid,42
ArrayTypeMismatchException
InvalidCastException
48
What does this code print?
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}");
2,3,2,4
6,3,2,4
6,3,5,4
6,2,2,4
49
What is the final content of numbers?
int[] numbers = { 1, 2, 3, 4, 5 };
Array.Copy(numbers, 0, numbers, 1, 4);
{ 1, 2, 2, 3, 4 }
{ 2, 3, 4, 5, 5 }
{ 1, 1, 2, 3, 4 }
{ 1, 1, 1, 1, 1 }
50
What does this code print on a modern .NET runtime?
string input = " a ,, b, ";
string[] parts = input.Split(
',',
StringSplitOptions.TrimEntries |
StringSplitOptions.RemoveEmptyEntries);
Console.Write($"{parts.Length}:{string.Join("|", parts)}");
3:a||b
4:a||b|
2:a|b
2: a | b
51
What does the following code print?
string first = "ab" + "cd";
string second = new string(new[] { 'a', 'b', 'c', 'd' });
Console.Write($"{first == second}:{ReferenceEquals(first, second)}");
True:False
True:True
False:True
False:False
52
What does this code print?
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());
PLR
PRR
PLL
PPP
53
What does this code print?
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);
2
1
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?
public int Value { get; private protected set; }
public int Value { get; internal set; }
public int Value { get; protected set; }
public int Value { get; protected internal set; }
55
What is the output when new Derived() is executed?
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");
}
}
Df Bf Bc Dc
Bf Bc Df Dc
Bf Df Dc Bc
Df Dc Bf Bc
56
What does this code print?
class Base
{
public string Show() => "Base";
}
class Derived : Base
{
public new string Show() => "Derived";
}
Base item = new Derived();
Console.Write(item.Show());
Derived
Base
BaseDerived
57
What does this code print?
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"));
Derived-string
Base-string
Derived-object
Base-object
58
What does this code print?
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());
A
C
B
59
What does the following code print?
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");
}
LFO
LFCO
LCF
FLO
60
What does this code print?
static int Calculate()
{
int value = 1;
try
{
return value;
}
finally
{
value = 2;
Console.Write(value);
}
}
Console.Write(Calculate());
21
22
11
12
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →