Unit 2: C# Programming Fundamentals - Practice Quiz
1 Which C# data type is commonly used to store a whole number?
int
string
double
bool
2
Which statement displays Hello World in a C# console application?
Console.GetLine("Hello World");
System.Print("Hello World");
Console.WriteLine("Hello World");
Console.ReadLine("Hello World");
3
What is the value of 2 + 3 * 4 in C#?
14
20
24
11
4 Which C# statement executes code only when a specified condition is true?
return
for
if
break
5 Which loop always executes its body at least once?
while loop
for loop
do-while loop
foreach loop
6 Which jump statement immediately exits the nearest loop?
goto
break
return
continue
7 Which declaration creates a one-dimensional integer array with five elements?
int values = new int[5];
int[] values = new int[5];
int[5] values = new int;
array<int> values = new[5];
8
Which expression returns the number of elements in an array named scores?
scores.Size
scores.Capacity
scores.Length
scores.Count
9
Which string method converts "hello" to "HELLO"?
ToUpper()
ToLower()
Trim()
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?
private
internal
public
protected
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?
virtual
private
static
sealed
17 Which statement about an abstract class in C# is correct?
18 Which block contains code that might cause an exception?
try
throw
finally
catch
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?
using
finally
checked
catch
21
What happens when the following C# code is compiled?
var quantity = 10;
quantity = 10.5;
quantity was inferred as int.
10.5 as a double.
10.
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#
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
11
12
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
B
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);
6
9
12
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);
}
012345
0134
013456
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]);
4
2
5
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]);
7
4
2
9
29
What is the output of the following string operation?
string text = "Modern Web";
Console.WriteLine(text.Substring(7, 3).ToUpper());
WEB
Web
ERN
MOD
30
What is printed by this code?
string language = "C#";
language.Replace("#", "Sharp");
Console.WriteLine(language);
C#
Sharp
C Sharp
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);
70
0
85
75
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();
Base Derived
Derived Base
Base Base
Derived Derived
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();
Animal
Dog
AnimalDog
37
Consider the following declaration:
abstract class Shape
{
public abstract double Area();
}
What must a non-abstract class derived from Shape do?
Area method.
Area with an implementation.
Area using the new keyword.
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");
}
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);
16
8
2
4
40
What does the following program print?
static int GetValue()
{
int value = 1;
try
{
return value;
}
finally
{
value = 2;
}
}
Console.WriteLine(GetValue());
2
1
0
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 7
0:0; exit code 0
0:7; exit code 0
0:7; exit code 7
43
What value is assigned to result?
int result = 1 + 2 * 3 << 1 & 15;
15
7
14
6
44
What does this code print?
int x = 0;
if (++x == 1 || ++x == 2 && ++x == 3)
{
x += 10;
}
Console.Write(x);
1
11
3
13
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);
4
3
10
6
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
DF1
D1F
FD1
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
InvalidCastException
ArrayTypeMismatchException
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}");
6,2,2,4
2,3,2,4
6,3,5,4
6,3,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, 1, 1, 1 }
{ 1, 1, 2, 3, 4 }
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)}");
2:a|b
4:a||b|
3: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:True
False:False
True:False
False:True
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());
PPP
PRR
PLR
PLL
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; internal set; }
public int Value { get; private protected 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");
}
}
Bf Df Dc Bc
Df Bf Bc Dc
Bf Bc Df Dc
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"));
Base-object
Derived-string
Derived-object
Base-string
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");
}
LFCO
LFO
FLO
LCF
60
What does this code print?
static int Calculate()
{
int value = 1;
try
{
return value;
}
finally
{
value = 2;
Console.Write(value);
}
}
Console.Write(Calculate());
12
21
11
22
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 →