Docsity

C# Past exam solution, Exams of Programming Languages

Toggle sidebar
bg1
Q1. Solve the following questions
a) Difference between .NET Framework and .NET Core
.NET Framework
.NET Core
Windows-only platform
Cross-platform (Windows, Linux, macOS)
Older framework
Modern and lightweight
Installed system-wide
Can be deployed with the application
Less suitable for cloud applications
Optimized for cloud and microservices
No longer actively developed for new features
Actively developed as .NET (5/6/7/8+)
b) What is Native AOT in .NET?
Native AOT (Ahead-of-Time Compilation) compiles a .NET application directly into native
machine code before execution.
Benefits:
• Faster startup time
• Lower memory usage
• No need for JIT compilation at runtime
• Better performance for console and cloud-native applications
c) Difference between an Array and a List in C#
Array
List
Fixed size
Dynamic size
Faster access
Slightly slower
Length cannot change
Can add/remove items
Uses Array class
Uses List<T> class
Less flexible
More flexible
Example:
int[] arr = {1,2,3};
List<int> list = new List<int>();
list.Add(1);
pf3
pf4
pf5

Partial preview of the text

Download C# Past exam solution and more Exams Programming Languages in PDF only on Docsity!

Q1. Solve the following questions

a) Difference between .NET Framework and .NET Core

.NET Framework .NET Core

Windows-only platform Cross-platform (Windows, Linux, macOS)

Older framework Modern and lightweight

Installed system-wide Can be deployed with the application

Less suitable for cloud applications Optimized for cloud and microservices

No longer actively developed for new features Actively developed as .NET (5/6/7/8+)

b) What is Native AOT in .NET?

Native AOT (Ahead-of-Time Compilation) compiles a .NET application directly into native

machine code before execution.

Benefits:

  • Faster startup time
  • Lower memory usage
  • No need for JIT compilation at runtime
  • Better performance for console and cloud-native applications

c) Difference between an Array and a List in C#

Array List

Fixed size Dynamic size

Faster access Slightly slower

Length cannot change Can add/remove items

Uses Array class Uses List class

Less flexible More flexible

Example:

int[] arr = {1,2,3}; List list = new List(); list.Add(1);

list.Add(2); d) How do you make a property read-only?

A property becomes read-only by providing only a get accessor.

public class Student { public string Name { get; } public Student(string name) { Name = name; } }

Or:

public int Age { get; private set; } e) Difference between Func, Action and Predicate delegates Action

  • Returns void
  • Can have 0–16 parameters Action print = s => Console.WriteLine(s); Func
  • Returns a value
  • Last type parameter is return type Func add = (a,b) => a+b; Predicate
  • Takes one parameter and returns bool Predicate isEven = x => x % 2 == 0;

Output

C - Length: 1 Sharp - Length: 5 is - Length: 2 a - Length: 1 powerful - Length: 8 programming - Length: 11 language - Length: 8

Q3. Dictionary Program

using System; using System.Collections.Generic; class Program { static void Main() { Dictionary> provinces = new Dictionary>(); provinces["Punjab"] = new List { "Lahore", "Rawalpindi", "Faisalabad", "Multan", "Bahawalpur", "Rahim Yar Khan", "Sargodha", "Gujranwala", "Gujrat" }; provinces["KPK"] = new List { "Peshawar", "Mardan", "DI Khan", "Abbottabad", "Kohat", "Bannu", "Swat" }; provinces["Balochistan"] = new List { "Quetta", "Gwadar", "Zhob", "Sibi", "Turbat"

provinces["Sindh"] = new List { "Karachi", "Hyderabad", "Sukkur", "Nawab Shah", "Sadiq Abad" }; foreach (KeyValuePair> province in provinces) { Console.WriteLine(province.Key); foreach (string city in province.Value) { Console.Write(city + ", "); } Console.WriteLine("\n"); } } } Expected Output Punjab Lahore, Rawalpindi, Faisalabad, Multan, Bahawalpur, Rahim Yar Khan, Sargodha, Gujranwala, Gujrat KPK Peshawar, Mardan, DI Khan, Abbottabad, Kohat, Bannu, Swat Balochistan Quetta, Gwadar, Zhob, Sibi, Turbat Sindh Karachi, Hyderabad, Sukkur, Nawab Shah, Sadiq Abad

These answers are exam-ready and can be written directly in your paper.