Programming 1B
Programming 1B
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 :
A. Dr.Gupta
B. Good Morning
C. Good Morning Dr.Gupta
D. Good Morning name
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:
(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
A.
while
{
}(condition);
B. while(condition)
{
};
C. while(condition)
{
D. while(condition);
{
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
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);
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;
}
}
}
}
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