0% found this document useful (0 votes)
79 views11 pages

BCA III Semester QP answers

lecture notes QP answers

Uploaded by

pankaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views11 pages

BCA III Semester QP answers

lecture notes QP answers

Uploaded by

pankaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Confidential - Oracle Restricted

Answers to Sample Question Paper 1

Part A: 1-Mark Questions

1. What is the default access modifier for class members in C#?


Answer: Private.

2. Define polymorphism in simple terms.


Answer: Polymorphism allows methods to take different forms, either
by overloading or overriding.

3. What does the is operator do in C#?


Answer: The is operator checks if an object is of a specific type.

4. What is the base class for all classes in C#?


Answer: System.Object.

Part B: 2-Mark Questions

(Attempt any 3 questions. Choose from the 5 given options.)

1. Explain the difference between == and = operators in C#.


Answer:

o ==: Compares two values for equality. Example: if (a == b).

o =: Assigns a value to a variable. Example: int x = 10;.

2. List two differences between an abstract class and an


interface.
Answer:

o Abstract classes can have method implementations; interfaces


cannot (prior to C# 8.0).

o A class can inherit only one abstract class but can implement
multiple interfaces.

3. Write the syntax of a for loop in C#. Provide an example.


Answer:
Syntax: for (initialization; condition; increment/decrement)
{ statements; }
Example:

csharp

Confidential - Oracle Restricted


Confidential - Oracle Restricted

Copy code

for (int i = 0; i < 5; i++) { Console.WriteLine(i); }

4. Define boxing and unboxing with an example.


Answer:

o Boxing: Converting a value type to an object. Example: object


obj = 10;.

o Unboxing: Extracting the value type from an object. Example:


int num = (int)obj;.

5. Explain the purpose of the default case in a switch statement.


Answer: The default case executes if no other case matches the input.
Example:

csharp

Copy code

switch (x) {

case 1: Console.WriteLine("One"); break;

default: Console.WriteLine("Other"); break;

Answers to Sample Question Paper 1

Part C: 5-Mark Questions

1. What is inheritance? Explain its purpose with an example.


Answer:
Inheritance is a fundamental concept of Object-Oriented Programming
(OOP) that allows a class (child) to reuse the properties and methods of
another class (parent). It helps in code reuse, establishing a parent-
child relationship, and promoting hierarchical structures in
applications.

Example:

csharp

Copy code

class Parent { public void Greet() => Console.WriteLine("Hello from


Parent"); }

Confidential - Oracle Restricted


Confidential - Oracle Restricted

class Child : Parent {}

class Program {

static void Main() {

Child child = new Child();

child.Greet(); // Output: Hello from Parent

Here, the Child class inherits the Greet method from the Parent class without
redefining it. This reduces duplication and simplifies maintenance.

2. Describe operator overloading with a simple example.


Answer:
Operator overloading allows customizing the behavior of operators (+,
-, *, etc.) for user-defined types. It enables developers to perform
intuitive operations directly on objects. For example, adding two
objects of a custom Complex class.

Example:

csharp

Copy code

class Complex {

public int Real { get; set; }

public int Imaginary { get; set; }

public static Complex operator +(Complex c1, Complex c2) {

return new Complex {

Real = c1.Real + c2.Real,

Imaginary = c1.Imaginary + c2.Imaginary

};

Confidential - Oracle Restricted


Confidential - Oracle Restricted

class Program {

static void Main() {

Complex c1 = new Complex { Real = 2, Imaginary = 3 };

Complex c2 = new Complex { Real = 1, Imaginary = 4 };

Complex result = c1 + c2;

Console.WriteLine($"Result: {result.Real} + {result.Imaginary}i");

Output: Result: 3 + 7i.


This demonstrates how the + operator is customized for Complex objects.

3. Write a program to demonstrate method overriding in C#.


Answer:
Method overriding allows a derived class to provide its own
implementation of a method defined in a base class. The base method
must be marked as virtual, and the derived method must use the
override keyword.

Example:

csharp

Copy code

class Animal {

public virtual void Speak() {

Console.WriteLine("Animal sound");

Confidential - Oracle Restricted


Confidential - Oracle Restricted

class Dog : Animal {

public override void Speak() {

Console.WriteLine("Bark");

class Program {

static void Main() {

Animal animal = new Dog();

animal.Speak(); // Output: Bark

Here, the Speak method in Dog overrides the Speak method in Animal to
provide a specific behavior for dogs.

4. Explain the difference between client-side and server-side


scripting with examples.
Answer:

 Client-Side Scripting: Runs on the user’s browser and is used for


creating interactive web pages. It reduces server load and provides
faster responses to user actions. Example: Form validation using
JavaScript.

html

Copy code

<script>

function validate() {

if (document.getElementById("name").value === "") alert("Name is


required");

Confidential - Oracle Restricted


Confidential - Oracle Restricted

</script>

<input type="text" id="name" />

<button onclick="validate()">Submit</button>

 Server-Side Scripting: Executes on the web server and is used for


dynamic content generation and database interactions. Example:
Fetching user details with PHP.

php

Copy code

<?php

$name = $_POST['name'];

echo "Welcome, $name";

?>

Both scripting types are essential for modern web applications to handle
frontend and backend tasks.

Confidential - Oracle Restricted


Confidential - Oracle Restricted

Answers to Sample Question Paper 2

Part A: 1-Mark Questions

1. What is an abstract class in C#?


Answer: A class that cannot be instantiated and contains at least one
abstract method.

2. Name two popular server-side scripting languages.


Answer: PHP and Python.

3. What is the use of the finally block in exception handling?


Answer: The finally block ensures that cleanup code is executed
regardless of whether an exception occurred.

4. Write an example of an entry-controlled loop in C#.


Answer:

csharp

Copy code

int i = 0;

while (i < 5) { Console.WriteLine(i); i++; }

Part B: 2-Mark Questions

(Attempt any 3 questions. Choose from the 5 given options.)

1. Explain the purpose of the Main() method in C#.


Answer: The Main() method is the entry point of a C# program where
execution starts. Example:

csharp

Copy code

static void Main(string[] args) { Console.WriteLine("Hello, World!"); }

2. What is method overloading? Provide an example.


Answer: Method overloading allows multiple methods with the same
name but different parameters. Example:

csharp

Copy code

Confidential - Oracle Restricted


Confidential - Oracle Restricted

void Print(int x);

void Print(string text);

3. State two advantages of client-side scripting.


Answer:

o Faster execution as it runs in the browser.

o Reduces server load by handling tasks like validation locally.

4. What is recursion? Provide a simple example.


Answer: Recursion is when a method calls itself to solve smaller
instances of a problem. Example:

csharp

Copy code

int Factorial(int n) => (n == 1) ? 1 : n * Factorial(n - 1);

5. Explain the difference between break and continue


statements.
Answer:

o break: Exits the loop entirely.

o continue: Skips the current iteration and moves to the next one.

Part C: 5-Mark Questions

1. Write a program to demonstrate the use of interfaces in C#.


Answer:
Interfaces define a contract with method signatures that a class must
implement. They support multiple inheritance.

Example:

csharp

Copy code

interface IAnimal {

void Speak();

Confidential - Oracle Restricted


Confidential - Oracle Restricted

class Dog : IAnimal {

public void Speak() {

Console.WriteLine("Bark");

class Program {

static void Main() {

IAnimal animal = new Dog();

animal.Speak(); // Output: Bark

Here, Dog implements the IAnimal interface by defining the Speak() method.
Interfaces allow flexibility and decouple method definitions from
implementations.

2. Explain exception handling in C# with the help of a try-catch-


finally example.
Answer:
Exception handling ensures that runtime errors are caught and handled
gracefully. The try block contains code that might throw an exception,
the catch block handles the exception, and the finally block executes
cleanup code regardless of exceptions.

Example:

csharp

Copy code

class Program {

static void Main() {

try {

Confidential - Oracle Restricted


Confidential - Oracle Restricted

int x = 10, y = 0;

Console.WriteLine(x / y); // Will throw DivideByZeroException

} catch (DivideByZeroException ex) {

Console.WriteLine("Error: Cannot divide by zero.");

} finally {

Console.WriteLine("Execution completed.");

Output:

vbnet

Copy code

Error: Cannot divide by zero.

Execution completed.

3. What is polymorphism? Explain its types with examples.


Answer:
Polymorphism enables methods to behave differently based on the
context.

 Compile-Time Polymorphism: Achieved via method overloading.


Example:

csharp

Copy code

class Calculator {

public int Add(int a, int b) => a + b;

public double Add(double a, double b) => a + b;

Confidential - Oracle Restricted


Confidential - Oracle Restricted

 Runtime Polymorphism: Achieved via method overriding.


Example:

csharp

Copy code

class Animal { public virtual void Speak() => Console.WriteLine("Animal


sound"); }

class Dog : Animal { public override void Speak() =>


Console.WriteLine("Bark"); }

4. Describe how the Concat() method works in C#. Write a


program to demonstrate it.
Answer:
The Concat() method joins two or more strings into a single string. It
can be used to combine string variables or literals.

Example:

csharp

Copy code

class Program {

static void Main() {

string s1 = "Hello";

string s2 = "World";

string result = string.Concat(s1, " ", s2);

Console.WriteLine(result); // Output: Hello World

This method is useful for constructing dynamic messages and combining


string data efficiently.

Confidential - Oracle Restricted

You might also like