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

Programming 1B

The document contains a series of multiple-choice questions and programming tasks related to C# and software testing. It includes questions on programming concepts, syntax, and control structures, as well as practical coding exercises for detecting key presses, calculating customer bills, and finding the greatest of three integers. Additionally, it discusses implicit vs explicit conversion and the importance of software testing prior to implementation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
6 views11 pages

Programming 1B

The document contains a series of multiple-choice questions and programming tasks related to C# and software testing. It includes questions on programming concepts, syntax, and control structures, as well as practical coding exercises for detecting key presses, calculating customer bills, and finding the greatest of three integers. Additionally, it discusses implicit vs explicit conversion and the importance of software testing prior to implementation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 11

SECTION A: [20 MARKS]

Multiple choice: Encircle the correct answer only


Each question carries [1 Mark]
1. ___________ translates high-level language program into machine language program.
A. The operating system
B. A compiler
C. The operating system
D. CPU
2. Which of these methods are used to read single character from the console?
A. get()
B. getline()
C. read()
D. readLine()

3. Which among the following does not belong to the C#.NET namespace?
A. class
B. struct
C. enum
D. Data
4. What is the output of following set of code ?
int a,b;
a = (b = 10) + 5;
A. b = 10, a = 5
B. b = 15, a = 5
C. a = 15, b = 10
D. a = 10, b = 10
5. Storage location used by computer memory to store data for usage by an application is ?
A. Pointers
B. Constants
C. Variable
D. None of the mentioned
6. Which of the following is the correct way to declare variables?
A. int length;, int width;
B. int length, width;
C. int length; width;
D. int length, int width;
7. To assign a value 1 to variable x, you write
A. 1 = x;
B. x = 1;
C. x := 1;
D. x == 1;

Page 1 of 11
8. Which of these data types requires the most amount of memory?
A. long
B. int
C. short
D. byte
9. Select output of the given set of Code :

static void Main(string[] args)

String name = "Dr.Gupta";

Console.WriteLine("Good Morning" + name);

A. Dr.Gupta
B. Good Morning
C. Good Morning Dr.Gupta
D. Good Morning name

10. Which of the following expression results in a value 1?


A. 2 % 1
B. 15 % 4
C. 25 % 5
D. 37 % 6
11. Choose selective differences between an array in c# and array in other programming
languages.
A. Declaring array in C# the square bracket([]) comes after the type but not after
identifier
B. It is not necessary to declare size of an array with its type
C. No difference between declaration of array in c# as well as as in other programming
languages
D. All of the above mentioned

12. Correct way to define a value 6.28 in a variable ‘a’ where value cannot be modified ?
A. #define a 6.28F
B. pi = 6.28F
C. const float pi = 6.28F
D. const float pi

Page 2 of 11
13. Analyze the following code:

boolean even = false;


if (even = true) {
Console.WriteLine("It is even!");
}
A. The program has a compile error.
B. The program has a runtime error.
C. The program runs, but displays nothing.
D. The program runs and displays It is even!.

14. What is the representation of the third element in an array called a?


A. a[2]
B. a(2)
C. a[3]
D. a(3)

15. Which of the following is correct?


A. int[] a = new int[2];
B. int a[] = new int[2];
C. int[] a = new int(2);
D. int a = new int[2];
16. What is the correct term for numbers[99]?
A. index
B. index variable
C. indexed variable
D. array variable
17. Do the following two statements in (I) and (II) result in the same value in sum?
(I):
for (int i = 0; i<10; ++i) {
sum += i;
}

(II):
for (int i = 0; i<10; i++) {
sum += i;
}
A. Yes
Page 3 of 11
B. No

18. Which of the following is not a namespace in the .NET Framework Class Library?
A. System.Process
B. System.Security
C. System.Threading
D. System.xml

19. Correct syntax for while statement is:

A.
while
{

}(condition);

B. while(condition)
{

};

C. while(condition)
{

D. while(condition);
{

20. Will the following program terminate?

int balance = 10;

while (true) {
if (balance < 9) break;
balance = balance - 9;
}
A. Yes
B. No

Page 4 of 11
Section B [20 marks]
Answer All Questions
QUESTION 1 [20 marks]
a) Differentiate between implicit and explicit conversion [6 marks]
Answer
Implicit Explicit

Implicit Conversion is done Explicit Conversion is done


automatically. programmatically.

In Implicit conversion, no data loss In explicit conversion, data loss may


takes place during the data or may not be take place during data
conversion. conversion. Hence there is a risk of
information loss.

No possibility of throwing exception It might throw error if tried to do


during the conversion and therefore without type casting.
is called type safe.

Implicit conversion do not require any Explicit conversion do require


special syntax cast operator to perform conversion.

b) Explain the reasons for testing a software prior to implementation [4 marks]


Answer
 Verification addresses the concern: "Are you building it right?"
 Ensures that the software system meets all the functionality.
 Verification takes place first and includes the checking for documentation, code, etc
 It has static activities, as it includes collecting reviews, walkthroughs, and inspections
to verify a software.
c) Describe the operation of selection program constructs: if, if…else, switch
[6 marks]
Answer
if statement – This is used to execute some code ONCE, only when a specified condition is
true.
if...else statement – This is an extension to the standard if statement. It allows to specify the
code that should run, if the condition in the first if statement evaluates to false.
if...else if....else statement – This is used to select from multiple different blocks of code. It
is often very similar to the switch statement which we shall cover soon.
Page 5 of 11
A switch statement allows use to execute code based on a predefined set choices.

d) Discuss in simple terms the functions of controls [4 marks]


Answer
A control structure is a primary concept in most high-level programming languages. In its
simplest sense, it is a block of code. More specifically, control structures are blocks of code
that dictate the flow of control. In other words, a control structure is a container for a series
of function calls, instructions and statements.
A simple example of a control structure is, if a then b else c. These statements will be
included or excluded based o-n the condition of a. This simple notion of if then comprises
the bulk of even the most sophisticated software applications.
There are many other types of control structures than the if then structure. In broad terms,
these include the jump or unconditional branch, the conditional branch, the loop, which is
essentially a conditional branch, subroutines, co-routines, continuations, and the conditional
halt. There are also lower level flow controls, such as interrupts and signals

Section C [60 marks]


Answer All Questions and save your answer on the desktop

QUESTION 1 [20 marks]

a) Write a C# program to detect key presses. If the user pressed number keys (from 0 to 9),
the program will display the number that is pressed, otherwise the program will show "Not
allowed". [10 marks]
Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{
char key;
Console.Write("Press a number key:");
key = (char)Console.Read();
switch (key)
{
case '0': Console.WriteLine("You pressed 0"); break;
Page 6 of 11
case '1': Console.WriteLine("You pressed 1"); break;
case '2': Console.WriteLine("You pressed 2"); break;
case '3': Console.WriteLine("You pressed 3"); break;
case '4': Console.WriteLine("You pressed 4"); break;
case '5': Console.WriteLine("You pressed 5"); break;
case '6': Console.WriteLine("You pressed 6"); break;
case '7': Console.WriteLine("You pressed 7"); break;
case '8': Console.WriteLine("You pressed 8"); break;
case '9': Console.WriteLine("You pressed 9"); break;
default: Console.WriteLine("Not allowed!"); break;
}
}
}
}
b) Write C# code to print the following pattern using a for loop: [10 marks]
1
12
123
1234
12345
123456
1234567
Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{
int i,j,k;
for (i = 1; i <= 7; i++)
{
Page 7 of 11
for (j = 1; j <= i; ++j)
Console.Write(j);

for (k = 7 - i; k >= 1; k--)


Console.Write("*");
Console.Write("\n");
}
Console.ReadLine();
}
}
}

QUESTION 2 [20 marks]

a) You are developer at MTC Namibia. You have been asked to create a program that will
calculate the total bill for 10 customers. The bill is determined by the number of minutes a
customer spend per day times the rate of N$2 per minute. Create a program to solve the
problem [10 marks]
b) Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication13
{
class Program
{
static void Main(string[] args)
{
double munites;
double bill;
double sum = 0d;
for (int i=1; i<10; i++)
{
Console.WriteLine("Please enter the munites for the next customer");
munites= Convert.ToDouble(Console.ReadLine());
bill =munites*2;

Page 8 of 11
sum=sum+bill;
}
Console.WriteLine("The bill for the customer is " + sum);
}
}
}
c) Write a C# program that allows the user to choose the correct answer of a question.
[10 marks]
See the example below:
What is the correct way to declare a variable to store an integer value in C#?
a. int 1x=10;
b. int x=10;
c. float x=10.0f;
d. string x="10";
Choose the answer : c
Incorrect choice!
Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("What is the correct way to declare a variable to store an integer value in
C#?");
Console.WriteLine("a. int 1x=10");
Console.WriteLine("b. int x=10");
Console.WriteLine("c. float x=10.0f");
Console.WriteLine("d. string x=\"10\"");
Console.WriteLine("Choose the answer letter:");
char ans = (char)Console.read();
switch (ans)
Page 9 of 11
{
case 'a':Console.WriteLine("Invalid choice!"); break;
case 'b':Console.WriteLine("Congratulation!"); break;
case 'c':Console.WriteLine("Invalid choice!"); break;
case 'd':Console.WriteLine("Invalid choice!"); break;
default:Console.WriteLine("Bad choice!");break;
}
}
}
}

QUESTION 3 [20 marks]

a) Write a C# program that prompts the user to input three integer values and find the greatest
value of the three values. [20 Marks]
Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{
int x,y,z;
Console.Write("Enter value 1:");
x = int.Parse(Console.ReadLine());
Console.Write("Enter value 2:");
y = int.Parse(Console.ReadLine());
Console.Write("Enter value 3:");
z = int.Parse(Console.ReadLine());
if (x > y)
if (x > z) Console.Write("The greatest value is:{0}.",x);
else Console.Write("The greatest value is:{0}.", z);
else if (y > z) Console.Write("The greatest value is:{0}.",y);

Page 10 of 11
else Console.Write("The greatest value is:{0}.",z);
Console.ReadLine();
}
}
}

Page 11 of 11

You might also like