Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

C# Program Output Questions

The document contains a series of C# programming questions and their expected outputs, covering fundamental topics such as data types, control flow, arrays, methods, object-oriented programming, error handling, and advanced topics. Each question is accompanied by a code snippet and the corresponding output that a candidate should expect when running the code. This collection serves as a resource for assessing a candidate's understanding of C# and their problem-solving skills.

Uploaded by

snehanila25
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

C# Program Output Questions

The document contains a series of C# programming questions and their expected outputs, covering fundamental topics such as data types, control flow, arrays, methods, object-oriented programming, error handling, and advanced topics. Each question is accompanied by a code snippet and the corresponding output that a candidate should expect when running the code. This collection serves as a resource for assessing a candidate's understanding of C# and their problem-solving skills.

Uploaded by

snehanila25
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 51

Question 1: Basic Data Types

csharp

Copy code

using System;

class Program

static void Main()

int a = 10;

double b = 5.5;

Console.WriteLine(a + b);

Expected Output:

Copy code

15.5

Question 2: String Concatenation

csharp

Copy code

using System;

class Program

static void Main()

string name = "World";


string greeting = "Hello " + name;

Console.WriteLine(greeting);

Expected Output:

Copy code

Hello World

Question 3: Conditional Statements

csharp

Copy code

using System;

class Program

static void Main()

int x = 10;

if (x > 5)

if (x < 15)

Console.WriteLine("Inside inner if");

else

Console.WriteLine("Inside outer else");

}
}

else

Console.WriteLine("Inside outer else");

Expected Output:

kotlin

Copy code

Inside inner if

Question 4: Looping

csharp

Copy code

using System;

class Program

static void Main()

for (int i = 0; i < 3; i++)

Console.Write(i + " ");

Expected Output:
Copy code

012

Question 5: Array Indexing

csharp

Copy code

using System;

class Program

static void Main()

int[] arr = { 1, 2, 3, 4, 5 };

Console.WriteLine(arr[2]);

Expected Output:

Copy code

Question 6: Value vs Reference Types

csharp

Copy code

using System;

class Program

static void Main()

{
int x = 5;

int y = x;

y = 10;

Console.WriteLine(x); // What will be the output?

Expected Output:

Copy code

Question 7: Exception Handling

csharp

Copy code

using System;

class Program

static void Main()

try

int[] numbers = { 1, 2, 3 };

Console.WriteLine(numbers[5]);

catch (IndexOutOfRangeException)

Console.WriteLine("Index out of range");

}
}

Expected Output:

sql

Copy code

Index out of range

Question 8: Class and Object

csharp

Copy code

using System;

class Person

public string Name { get; set; }

public Person(string name)

Name = name;

class Program

static void Main()

Person p = new Person("Alice");

Console.WriteLine(p.Name);
}

Expected Output:

Copy code

Alice

Question 9: Static vs Non-Static Methods

csharp

Copy code

using System;

class MyClass

public static void StaticMethod()

Console.WriteLine("Static Method");

public void InstanceMethod()

Console.WriteLine("Instance Method");

class Program

static void Main()

{
MyClass.StaticMethod();

// MyClass.InstanceMethod(); // Uncommenting this line will cause a


compilation error

Expected Output:

sql

Copy code

Static Method

Question 10: Overloading Methods

csharp

Copy code

using System;

class MyClass

public void Print(int number)

Console.WriteLine("Number: " + number);

public void Print(string text)

Console.WriteLine("Text: " + text);

}
class Program

static void Main()

MyClass obj = new MyClass();

obj.Print(5);

obj.Print("Hello");

Expected Output:

makefile

Copy code

Number: 5

Text: Hello

These questions cover a range of fundamental topics in C#, from basic


syntax and control structures to object-oriented principles. They are useful
for assessing a candidate's basic understanding of the language and their
problem-solving skills.

still 50 questions

ChatGPT

Certainly! Here is a comprehensive list of 50 C# program output questions.


These cover various aspects of the language, including data types, control
flow, collections, and object-oriented programming.

Basic Data Types and Operations

1. Integer Operations

csharp

Copy code

using System;
class Program

static void Main()

int a = 8;

int b = 3;

Console.WriteLine(a / b);

Expected Output:

Copy code

2. Floating Point Precision

csharp

Copy code

using System;

class Program

static void Main()

double a = 5.0 / 3.0;

Console.WriteLine(a);

Expected Output:
Copy code

1.6666666666666667

3. Boolean Logic

csharp

Copy code

using System;

class Program

static void Main()

bool x = true;

bool y = false;

Console.WriteLine(x && y);

Expected Output:

graphql

Copy code

False

4. String Concatenation

csharp

Copy code

using System;

class Program

{
static void Main()

string a = "Hello";

string b = "World";

Console.WriteLine(a + " " + b);

Expected Output:

Copy code

Hello World

5. String Interpolation

csharp

Copy code

using System;

class Program

static void Main()

string name = "Alice";

int age = 30;

Console.WriteLine($"Name: {name}, Age: {age}");

Expected Output:

yaml

Copy code
Name: Alice, Age: 30

Control Flow

6. If-Else Statement

csharp

Copy code

using System;

class Program

static void Main()

int x = 10;

if (x > 5)

Console.WriteLine("Greater");

else

Console.WriteLine("Smaller");

Expected Output:

mathematica

Copy code

Greater

7. Nested If Statements
csharp

Copy code

using System;

class Program

static void Main()

int x = 20;

if (x > 10)

if (x < 30)

Console.WriteLine("Between 10 and 30");

Expected Output:

mathematica

Copy code

Between 10 and 30

8. Switch Statement

csharp

Copy code

using System;
class Program

static void Main()

int day = 3;

switch (day)

case 1:

Console.WriteLine("Monday");

break;

case 2:

Console.WriteLine("Tuesday");

break;

case 3:

Console.WriteLine("Wednesday");

break;

default:

Console.WriteLine("Weekend");

break;

Expected Output:

mathematica

Copy code

Wednesday

9. For Loop
csharp

Copy code

using System;

class Program

static void Main()

for (int i = 0; i < 3; i++)

Console.Write(i + " ");

Expected Output:

Copy code

012

10. While Loop

csharp

Copy code

using System;

class Program

static void Main()

int i = 0;
while (i < 3)

Console.Write(i + " ");

i++;

Expected Output:

Copy code

012

Arrays and Collections

11. Array Access

csharp

Copy code

using System;

class Program

static void Main()

int[] numbers = { 1, 2, 3, 4, 5 };

Console.WriteLine(numbers[3]);

Expected Output:

Copy code

4
12. Array Iteration

csharp

Copy code

using System;

class Program

static void Main()

int[] numbers = { 1, 2, 3, 4, 5 };

foreach (var number in numbers)

Console.Write(number + " ");

Expected Output:

Copy code

12345

13. Array Initialization

csharp

Copy code

using System;

class Program

static void Main()


{

int[] numbers = new int[3];

numbers[0] = 10;

numbers[1] = 20;

numbers[2] = 30;

Console.WriteLine(numbers[1]);

Expected Output:

Copy code

20

14. List Usage

csharp

Copy code

using System;

using System.Collections.Generic;

class Program

static void Main()

List<int> numbers = new List<int> { 1, 2, 3 };

numbers.Add(4);

Console.WriteLine(numbers.Count);

Expected Output:
Copy code

15. List Iteration

csharp

Copy code

using System;

using System.Collections.Generic;

class Program

static void Main()

List<string> names = new List<string> { "Alice", "Bob", "Charlie" };

foreach (string name in names)

Console.Write(name + " ");

Expected Output:

Copy code

Alice Bob Charlie

Functions and Methods

16. Simple Method

csharp

Copy code

using System;
class Program

static void PrintMessage()

Console.WriteLine("Hello World");

static void Main()

PrintMessage();

Expected Output:

Copy code

Hello World

17. Method with Parameters

csharp

Copy code

using System;

class Program

static void Greet(string name)

Console.WriteLine("Hello, " + name);

}
static void Main()

Greet("Alice");

Expected Output:

Copy code

Hello, Alice

18. Method with Return Value

csharp

Copy code

using System;

class Program

static int Add(int a, int b)

return a + b;

static void Main()

Console.WriteLine(Add(5, 3));

Expected Output:
Copy code

19. Method Overloading

csharp

Copy code

using System;

class Program

static void Display(int value)

Console.WriteLine("Integer: " + value);

static void Display(string value)

Console.WriteLine("String: " + value);

static void Main()

Display(42);

Display("Hello");

Expected Output:

makefile
Copy code

Integer: 42

String: Hello

20. Recursive Method

csharp

Copy code

using System;

class Program

static int Factorial(int n)

if (n <= 1) return 1;

return n * Factorial(n - 1);

static void Main()

Console.WriteLine(Factorial(5));

Expected Output:

Copy code

120

Object-Oriented Programming

21. Class Definition

csharp
Copy code

using System;

class Person

public string Name;

public void Greet()

Console.WriteLine("Hello, " + Name);

class Program

static void Main()

Person p = new Person();

p.Name = "Alice";

p.Greet();

Expected Output:

Copy code

Hello, Alice

22. Constructor Usage

csharp
Copy code

using System;

class Person

public string Name;

public Person(string name)

Name = name;

public void Greet()

Console.WriteLine("Hello, " + Name);

class Program

static void Main()

Person p = new Person("Bob");

p.Greet();

Expected Output:
Copy code

Hello, Bob

23. Inheritance

csharp

Copy code

using System;

class Animal

public void Eat()

Console.WriteLine("Eating");

class Dog : Animal

public void Bark()

Console.WriteLine("Barking");

class Program

static void Main()

{
Dog d = new Dog();

d.Eat();

d.Bark();

Expected Output:

Copy code

Eating

Barking

24. Polymorphism

csharp

Copy code

using System;

class Animal

public virtual void Speak()

Console.WriteLine("Animal sound");

class Dog : Animal

public override void Speak()

Console.WriteLine("Woof");
}

class Program

static void Main()

Animal a = new Dog();

a.Speak();

Expected Output:

Copy code

Woof

25. Abstract Class

csharp

Copy code

using System;

abstract class Shape

public abstract void Draw();

class Circle : Shape

public override void Draw()


{

Console.WriteLine("Drawing a Circle");

class Program

static void Main()

Shape s = new Circle();

s.Draw();

Expected Output:

css

Copy code

Drawing a Circle

Error Handling

26. Try-Catch Block

csharp

Copy code

using System;

class Program

static void Main()

{
try

int[] numbers = { 1, 2, 3 };

Console.WriteLine(numbers[5]);

catch (IndexOutOfRangeException)

Console.WriteLine("Index out of range");

Expected Output:

sql

Copy code

Index out of range

27. Finally Block

csharp

Copy code

using System;

class Program

static void Main()

try

Console.WriteLine("In try block");


}

finally

Console.WriteLine("In finally block");

Expected Output:

csharp

Copy code

In try block

In finally block

28. Exception Propagation

csharp

Copy code

using System;

class Program

static void Function1()

throw new Exception("Error in Function1");

static void Function2()

try
{

Function1();

catch (Exception ex)

Console.WriteLine("Caught in Function2: " + ex.Message);

static void Main()

Function2();

Expected Output:

javascript

Copy code

Caught in Function2: Error in Function1

29. Custom Exception

csharp

Copy code

using System;

class MyCustomException : Exception

public MyCustomException(string message) : base(message)

{
}

class Program

static void Main()

try

throw new MyCustomException("This is a custom exception");

catch (MyCustomException ex)

Console.WriteLine(ex.Message);

Expected Output:

vbnet

Copy code

This is a custom exception

30. Checked vs Unchecked

csharp

Copy code

using System;

class Program
{

static void Main()

unchecked

int max = int.MaxValue;

int result = max + 1;

Console.WriteLine(result);

Expected Output:

diff

Copy code

-2147483648

Advanced Topics

31. Lambda Expressions

csharp

Copy code

using System;

class Program

static void Main()

Func<int, int> square = x => x * x;

Console.WriteLine(square(5));
}

Expected Output:

Copy code

25

32. Anonymous Types

csharp

Copy code

using System;

class Program

static void Main()

var person = new { Name = "Alice", Age = 30 };

Console.WriteLine(person.Name + " is " + person.Age + " years old");

Expected Output:

csharp

Copy code

Alice is 30 years old

33. Extension Methods

csharp

Copy code

using System;
static class StringExtensions

public static string ToUppercase(this string str)

return str.ToUpper();

class Program

static void Main()

string greeting = "hello";

Console.WriteLine(greeting.ToUppercase());

Expected Output:

Copy code

HELLO

34. Delegates

csharp

Copy code

using System;

delegate void DisplayMessage(string message);

class Program
{

static void Show(string message)

Console.WriteLine(message);

static void Main()

DisplayMessage del = Show;

del("Hello, Delegate!");

Expected Output:

vbnet

Copy code

Hello, Delegate!

35. Events

csharp

Copy code

using System;

class Publisher

public event Action Notify;

public void RaiseEvent()

{
Notify?.Invoke();

class Program

static void Main()

Publisher pub = new Publisher();

pub.Notify += () => Console.WriteLine("Event triggered");

pub.RaiseEvent();

Expected Output:

vbnet

Copy code

Event triggered

Additional Questions

36. Null Coalescing Operator

csharp

Copy code

using System;

class Program

static void Main()

{
string name = null;

Console.WriteLine(name ?? "Default Name");

Expected Output:

mathematica

Copy code

Default Name

37. Null Conditional Operator

csharp

Copy code

using System;

class Program

static void Main()

string name = null;

int? length = name?.Length;

Console.WriteLine(length);

Expected Output:

csharp

Copy code

<null>

38. Using Statement


csharp

Copy code

using System;

using System.IO;

class Program

static void Main()

using (StreamWriter writer = new StreamWriter("test.txt"))

writer.WriteLine("Hello, file!");

Expected Output: (No console output, but a file test.txt is created with
"Hello, file!")

39. Properties

csharp

Copy code

using System;

class Person

private string name;

public string Name


{

get { return name; }

set { name = value; }

class Program

static void Main()

Person p = new Person();

p.Name = "Alice";

Console.WriteLine(p.Name);

Expected Output:

Copy code

Alice

40. Indexers

csharp

Copy code

using System;

class MyCollection

private string[] data = new string[3];


public string this[int index]

get { return data[index]; }

set { data[index] = value; }

class Program

static void Main()

MyCollection collection = new MyCollection();

collection[0] = "Hello";

Console.WriteLine(collection[0]);

Expected Output:

Copy code

Hello

41. Tuples

csharp

Copy code

using System;

class Program

static void Main()


{

var tuple = (Name: "Alice", Age: 30);

Console.WriteLine(tuple.Name + " is " + tuple.Age + " years old");

Expected Output:

csharp

Copy code

Alice is 30 years old

42. Anonymous Methods

csharp

Copy code

using System;

class Program

static void Main()

Action<string> greet = delegate (string name)

Console.WriteLine("Hello, " + name);

};

greet("Bob");

Expected Output:
Copy code

Hello, Bob

43. Yield Keyword

csharp

Copy code

using System;

using System.Collections.Generic;

class Program

static IEnumerable<int> GenerateNumbers()

for (int i = 0; i < 5; i++)

yield return i;

static void Main()

foreach (int number in GenerateNumbers())

Console.Write(number + " ");

Expected Output:
Copy code

01234

44. Dynamic Type

csharp

Copy code

using System;

class Program

static void Main()

dynamic obj = "Hello";

Console.WriteLine(obj.Length);

Expected Output:

Copy code

45. Nullable Types

csharp

Copy code

using System;

class Program

static void Main()

{
int? num = null;

Console.WriteLine(num.HasValue);

Console.WriteLine(num.GetValueOrDefault());

Expected Output:

graphql

Copy code

False

46. String Format

csharp

Copy code

using System;

class Program

static void Main()

string name = "Alice";

int age = 30;

Console.WriteLine(string.Format("Name: {0}, Age: {1}", name, age));

Expected Output:

yaml

Copy code
Name: Alice, Age: 30

47. DateTime Formatting

csharp

Copy code

using System;

class Program

static void Main()

DateTime now = DateTime.Now;

Console.WriteLine(now.ToString("yyyy-MM-dd"));

Expected Output:

bash

Copy code

(current date in yyyy-MM-dd format)

48. Enum Usage

csharp

Copy code

using System;

enum Days

Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday

}
class Program

static void Main()

Days today = Days.Wednesday;

Console.WriteLine(today);

Expected Output:

mathematica

Copy code

Wednesday

49. ReadOnly Fields

csharp

Copy code

using System;

class MyClass

public readonly int Value;

public MyClass(int value)

Value = value;

}
class Program

static void Main()

MyClass obj = new MyClass(42);

Console.WriteLine(obj.Value);

Expected Output:

Copy code

42

50. Static Constructors

csharp

Copy code

using System;

class MyClass

static MyClass()

Console.WriteLine("Static constructor");

public MyClass()

Console.WriteLine("Instance constructor");
}

class Program

static void Main()

MyClass obj1 = new MyClass();

MyClass obj2 = new MyClass();

Expected Output:

kotlin

Copy code

Static constructor

Instance constructor

Instance constructor

You might also like