0% found this document useful (0 votes)
45 views33 pages

Console Questions and Answers Programming

Uploaded by

ngaralydiah537
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)
45 views33 pages

Console Questions and Answers Programming

Uploaded by

ngaralydiah537
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/ 33

1

PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

nvbgvVisual Basic.NET Console Book


Programming questions and suggested solutions
calculations

1. Write a program that perform the following


a) Addition
b) Subtraction
c) Division
d) Multiplication

 Solution

Module Module1

Sub Main()
'declaring variables
Dim num1, num2, a, b, c, d As Double
'inputing figures
Console.WriteLine("plizz enter the first number")
num1 = Console.ReadLine()
Console.WriteLine("plizz enter the second number")
num2 = Console.ReadLine()
'displaying the answer
Console.WriteLine("the answer is")
'adding two numbers
a = (num1 + num2)
Console.WriteLine(a)
'multiplying two numbers
b = (num1 * num2)
Console.WriteLine(b)
'dividing two numbers
c = (num1 / num2)
Console.WriteLine(c)
'subtracting two numbers
d = (num1 - num2)
Console.WriteLine(d)

Console.ReadKey()
2
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

End Sub

End Module

2. Write a program I console that allows the user to enter his/her name and grade, the
program should be able to display the name and grade entered
 Solution

Module Module1

Sub Main()
Dim name As String
Dim grade As Integer
Console.WriteLine("what is your name")
name = Console.ReadLine()
Console.WriteLine("what is your grade")
grade = Console.ReadLine()
Console.WriteLine("l am " & name & vbTab & grade)
Console.ReadKey()

End Sub

End Module
3
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

3. Write a program that display the name and surname that the user entered
Solution

Module Module1

Sub Main() 'done by tanaka tanya penia


Dim forename, surname As String
Console.WriteLine("hello good day ,what is ur name?")
forname = Console.ReadLine()
Console.WriteLine("hello how are you ,what is your
surname?")
surname = Console.ReadLine()
Console.WriteLine(forname & vbTab & surname)
Console.ReadKey()
End Sub

End Module
4
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

3. Write a program that calculates the mode

 solution

Module Module1

Sub Main()
Dim a, b, c, d As Double
Console.WriteLine("please enter first number")
a = Console.ReadLine()
Console.WriteLine("please enter the second number")
b = Console.ReadLine()
Console.WriteLine("the answer is")
c = (a Mod b)
Console.WriteLine(c)
d = (a \ b)
Console.WriteLine(d)

Console.ReadKey()

End Sub

End Module

4. Write a program that calculates the area and perimeter of a circle


5
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

 solution

Module Module1

Sub Main()
Const PI As Single = 22 / 7
Dim rad, answ As Double
Console.WriteLine("please enter the radius")
rad = Console.ReadLine()
Console.WriteLine("the answer is")
answ = (PI * rad ^ 2)
Console.WriteLine(answ)
answ = (2 * PI * rad)
Console.WriteLine(answ)
Console.ReadKey()

End Sub

End Module

5. Write a program that calculates the volume of a rectangular swimming pool


6
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

 Solution

Module Module1

Sub Main()
Dim l, w, h As Single
Dim volume As Double
Console.WriteLine("please enter length in metres")
l = Console.ReadLine()
Console.WriteLine("please enter width in metres")
w = Console.ReadLine()
Console.WriteLine("please enter height in metres")
h = Console.ReadLine()
Console.WriteLine("the volume of the water is")
volume = (l * w * h)
Console.WriteLine(volume)
volume = Console.ReadLine()
Console.ReadKey()

End Sub

End Module

6. Write a program that convert the temperature entered in degrees Celsius to Fahrenheit
7
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

 Solution

Module Module1

Sub Main()
Dim celcius As Single
Dim f As Double
Console.WriteLine("Please enter temperature in degrees
celcius")
celcius = Console.ReadLine()
Console.WriteLine("the temperature in Fahrenheit")
f = ((9 / 5 * celcius) + 32)
Console.WriteLine(f)
f = Console.ReadLine()
Console.ReadKey()

End Sub

End Module

7. Write a program that convert the temperature entered in Fahrenheit to degrees Celsius.
8
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

Module Module1

Sub Main()
Dim fehnr, degrees As Double
Console.WriteLine("Please enter the temperature in
fahrenheight")
fehnr = Console.ReadLine()
Console.WriteLine("Phe temperature in dregrees
celcious is")
degrees = (5 * (fehnr - 32)) / 9
Console.WriteLine(degrees)
Console.WriteLine("The answer correct to two decimal
place is " & Format(degrees, "####.00"))
Console.ReadKey()

End Sub

End Module

Solution

8. You are asked to design console application the can convert currency from USD$ to RTGS$
9
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

Imports System.Math
Module Module1

Sub Main()
Dim amt1, amt2, RTGS As Single
Console.WriteLine("Enter rate equivalent to 1 USD$ in
RTGS$")
amt1 = Console.ReadLine()
Console.WriteLine("Enter amount to be converted to RTGS$ in
USD$")
amt2 = Console.ReadLine()
RTGS = (amt1 * amt2)
Console.WriteLine("Equivalent amount is " & Format(RTGS,
"RTGS$ ####.00"))
Console.ReadKey()

End Sub

End Module

Solution
10
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

9. You are asked to design a console application that displays random number between 0 to
10 until 10 is generated

Imports System.Math
Module Module1

Sub Main()
Console.WriteLine("Random numbers 1 to 10")
Console.WriteLine(Math.Ceiling(Rnd() * 10))
Console.ReadLine()
Console.ReadKey()
End Sub

End Module

10. Write a program that determine whether the mark entered by the user is a pass or a fail. It
should display the output in a message box
11
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

Imports System.Math
Module Module1

Sub Main()
Dim Mark As Integer
Console.WriteLine("Enter your mark")
Mark = Console.ReadLine()
If Mark > 100 Then
MsgBox("invalid !!!")
Else
If Mark < 0 Then
MsgBox("You have entered an invalid mark Please try entering
your details correctly!!!")
Else
If Mark >= 50 Then
MsgBox("Pass well done keep it up")
Else
If Mark <= 49 Then
MsgBox("You have failed")
Else

End If
End If
End If
End If
Console.ReadKey()
End Sub

End Module

11. Write a program that allows the user to enter the marks of students and gives the grade eg
if the user enters 30 it then displays grade F.
Module Module1

Sub Main()
Dim mark As Integer
Console.WriteLine("please enter your mark")
mark = Console.ReadLine
Select Case mark
Case 70 To 100
Console.WriteLine("grade A")
Case 60 To 69
Console.WriteLine("grade B")
Case 50 To 59
Console.Write("grade C")
Case 40 To 49
Console.WriteLine("grade D")
Case 30 To 39
Console.WriteLine("grade E")
Case 0 To 29
Console.WriteLine("grade F")

End Select
Console.ReadLine
12
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

Solution

12. Write a console application that asks the user to enter the month and the application
should be able to display the number of days in that month
Module Module1

Sub Main()
Dim month As Integer
Console.WriteLine("Please anter a month")
month = Console.ReadLine()
Select Case month
Case 1, 3, 5, 7, 8, 10
Console.WriteLine("31 days")
Case 4, 6, 9, 11
Console.WriteLine("30 days")
Case 2
Console.WriteLine("28 days")
Case Else
Console.WriteLine("no such month")

End Select
Console.ReadKey()
End Sub

End Module

Solution
13
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

13. You are asked to write a console application that gives the user ability to enter the month
and displays the number of days in that month for example if the user enter July then it
will display 30 days.
Module Module1

Sub Main()
Dim month As String
Console.WriteLine("Enter month of the year")
month = Console.ReadLine()

Select Case month


Case "january"
Console.WriteLine("30 days")
Case "february"
Console.WriteLine("28 days")
Case "march"
Console.WriteLine("31 days")
Case "april"
Console.WriteLine("30 days")
Case "may"
Console.WriteLine("31 days")
Case "june"
Console.WriteLine("30 days")
Case "july"
Console.WriteLine("31 days")
Case "august"
Console.WriteLine("31 days")
Case "september"
Console.WriteLine("30 days")
Case "october"
Console.WriteLine("31 days")
Case "november"
Console.WriteLine("30 days")
Case "december"
Console.WriteLine("31 days")
Case Else
Console.WriteLine("invalid input !!! please retry entering
correct details")
End Select
Console.ReadKey()

End Sub

End Module

Solution
14
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

14. Create a VB.Net using the select case statements that displays a message box depending
on the day the temperature input, using the following
Temperature <=0
Temperature >= and <= 20 moderate
>= 21 hot

Solution

Module Module1

Sub Main()
Dim temp As Integer
Console.WriteLine("PLEASE INPUT TEMPERATURE IN DEGREES CELCIUS ")
temp = Console.ReadLine()
Select Case temp
Case Is <= 0
MsgBox("FREEZING")
Case 1 To 20
MsgBox("MODERATE")
Case Is >= 21
MsgBox("HOT")
Case Else
MsgBox("INVALID DATA ENTERED PLEASE RETRY ENTERING CORRECTLY")
End Select
Console.ReadKey()
End Sub

End Module
15
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

15. Write a console application that allows the user to enter a string and program will check
whether the word entered is a palindrome or not.
A palindrome is a word, phrase, number, or other sequence of characters which reads the
sane backward or forward.
For example – mom, dad, wow, madam, radar, level, eye.

Module Module1

Sub Main()
Dim name, pname As String
Console.WriteLine("please enter the word")
name = Console.ReadLine()
'now i am declaring a function that reverse the name
entered
pname = StrReverse(name)
If name = pname Then
Console.WriteLine("it is a palindrome")
Else
Console.WriteLine("it is not a palindrome")
End If
Console.ReadKey()
End Sub

End Module

Solution
16
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

16. Write a program that calculates the minimum, maximum and average of two numbers that
is entered by a user. [10]
Imports System.Math
Module Module1

Sub Main()
Dim num1, num2, avg, mini, maxi As Double
Console.WriteLine("Please enter the first number")
num1 = Console.ReadLine()
Console.WriteLine("please enter the second number")
num2 = Console.ReadLine()
Console.WriteLine("maximum")
maxi = Math.Max(num1, num2)
Console.WriteLine(maxi)
Console.WriteLine("minimum")
mini = Math.Min(num1, num2)
Console.WriteLine(mini)
Console.WriteLine("the average is")
avg = (num1 + num2) / 2
Console.WriteLine(avg)
Console.ReadKey()
End Sub

End Module

Solution
17
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

17. Write a code that compare 3 numbers through a keyboard and output the smallest, highest and
average of numbers entered. [10]

Module Module1

Sub Main()
Dim num1, num2, num3 As Integer
Console.WriteLine("please enter first number")
num1 = Console.ReadLine()
Console.WriteLine("please enter the second")
num2 = Console.ReadLine()
Console.WriteLine("please enter the third number")
num3 = Console.ReadLine()
If num1 > num2 And num1 > num3 Then
Console.WriteLine(num1 & " is the greatest")
ElseIf num2 > num3 And num2 > num1 Then
Console.WriteLine(num2 & " is the greatest")
ElseIf num3 > num2 And num3 > num1 Then
Console.WriteLine(num3 & " is the greatest ")
End If

If num1 < num2 And num1 < num3 Then


Console.WriteLine(num1 & " is the smallest")
ElseIf num2 < num3 And num2 < num1 Then
Console.WriteLine(num2 & " is the smallest")
ElseIf num3 < num2 And num3 < num1 Then
Console.WriteLine(num3 & " is the smallest ")
Else
Console.WriteLine("they are equal")
End If
Console.ReadKey()
End Sub

End Module

Solution
18
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

18. By using for loop structure write a program that displays a sequence of numbers with a
common different of 2

NB. Common different is found after subtracting successive term and previous term

Module Module1

Sub Main()
Dim num1 As Integer
Console.WriteLine("display a sequence of numbers ")
For num1 = 1 To 30 Step 2
Console.WriteLine(num1)
Console.ReadKey()
Next
End Sub

End Module

Solution
19
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

19. Using a looping structure of your choice, write a program which outputs the following
result.

Module Module1

Sub Main()
Dim x, y As Integer
Dim n As Integer = 12
For y = 1 To 12 'the first FOR loop determines the y-
axis
For x = 1 To n 'the second FOR loop determines the
x-xis
Console.Write("*") 'this displays the text
inside the quotation marks but does not give a new line
afterwards.
Next
n = n - 1
Console.WriteLine()
Next
Console.ReadKey()

End Sub
End Module
OR
Module Module1

Sub Main()
Dim n, j As Integer
For n = 1 To 12
For j = 1 To 12 - n
Console.Write("*")

Next
Console.WriteLine()
Next
20
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

Console.ReadKey()
End Sub
End Module

(b) Modify the code you wrote in (a) so that it prints the shape upside down. [6]

Module Module1

Sub Main()
Dim x, y As Integer
Dim n As Integer = 1
For y = 1 To 12 'the first for loop determines the y-axis
For x = 1 To n 'the second for loop determines the x-
axis
Console.Write("*")
Next
n = n + 1
Console.WriteLine()
Next
Console.ReadKey()

End Sub

End Module

OR

Module Module1

Sub Main()
Dim n, j As Integer
For n = 1 To 12
For j = 1 To n
Console.Write("*")
Next
Console.WriteLine()
21
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

Next
Console.ReadKey()
End Sub

End Module

20. Using a looping structure of your choice, write a program which outputs the following
result.

Module Module1

Sub Main()
Dim x, y, z, a As Integer
For x = 1 To 12
For y = 12 To x Step -1
Console.Write(" ")
Next y
For z = 1 To x
Console.Write("*")
Next
For a = 2 To x
Console.Write("*")

Next
Console.WriteLine()

Next
Console.ReadKey()
End Sub

End Module
22
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

OR
Module Module1

Sub Main()
Dim count As Integer = 1
Dim height As Integer = 12
Dim space As Integer = 12
For row = 1 To 12
For gap = space To row Step -1
Console.Write(" ")
Next gap
For column = 1 To count
Console.Write("*")
Next column
count = count + 2
Console.WriteLine()
Next row
Console.ReadKey()
End Sub

End Module

21. Write a program that displays the following output


23
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

Module Module1

Sub Main()
Dim x, y, z, a As Integer
For x = 1 To 12
For y = 12 To x Step -1
Console.Write(" ")
Next y
For z = 1 To x
Console.Write("*")
Next
For a = 1 To x
Console.Write("#")

Next
Console.WriteLine()

Next
Console.ReadKey()
End Sub

End Module

22. Write a program which inputs 10 numbers and finally outputs the summation of these
numbers. [10]

Module Module1

Sub Main()
Dim num As Integer = 0
Dim total As Integer
Console.WriteLine("the sum of all integers between 1
to 10")
Do While num <= 10
total = total + num
num = num + 1
Loop
Console.WriteLine(total)
Console.ReadKey()
24
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

End Sub

End Module

Screen Shot

(b) Using a trace table, dry run the code. [8]

Solution

Trace Table

Num Total Output


0 0
1 1
2 3
3 6
4 10
5 15
6 21
7 28
8 36
9 45
10 55 55
25
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

Writing algorithms in Pseudocode


Writing an algorithm in pseudocode is no longer graphical like a program flowchart, but is
one step closer to writing program code in a high-level language.
Producing an algorithm for a solution in pseudocode typically includes:
Declaring variables and constants
Initialising any variables for total, count, highest and lowest values
Input values
Using an appropriate loop structure for repetitions of data entry and/or other processing
Using conditional statements to select appropriate processing alternatives
Output the algorithm.

Testing and interpreting pseudocode algorithms


Dry running a pseudocode algorithm with a trace table and test data helps to understand its
behaviour and purpose.
Line 1 Exponent ← 0
Line 2 REPEAT
Line 3 Result ← 2 ^ Exponent
Line 4 PRINT "2^", Exponent, " = ", Result
Line 5 Exponent ← Exponent + 1
Line 6 UNTIL Result > 100
What does this pseudocode algorithm do?

Trace Table
Trace tables are used to dry run of algorithm for testing.
Trace Table has columns for all variables, logical expressions and output.

Exponent Result OUTPUT Description


0 Initialisation of variable
1 1 2^0=1 1st iteration
2 2 2^1=2 2nd iteration
3 4 2^2=4 3rd iteration
4 8 2^3=8 4th iteration
5 16 2^4=16 5th iteration
6 32 2^5=32 6th iteration
26
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

7 64 2^6=64 7th iteration


8 128 2^7=128 8th iteration

Example interpretation
The purpose of the algorithm is to print a list of the powers of 2 starting at 20 until it reaches
the first one over 100.

23. Produce a trace table for the following algorithm using the number 8271. [4]

1 INPUT N
2 LET x = INT(N / 10)
3 PRINT N – 10 * x
4 IF x = 0 THEN STOP
5 N=x
6 GOTO 2
(b) Deduce the purpose of the algorithm. [1]

(a) Solution

Trace Table

Number x N
8 271 827 1
82 7 [1]
8 2 [1]
0 8
[1]

[1]
( 1 mark for each row)

(b) Outputs digits of number (N) in reverse order. [1]

24. (a) A computer program is required which inputs 10 numbers, multiplies them together
and finally outputs the answer (the product). The following algorithm has been written to do
this.

1 count = 0
2 product = 0
3 while count <= 10 do
4 input number
5 product = product * number
6 count = count + 1
27
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

7 print product
8 endwhile

There are three errors in the algorithm. Locate and describe these errors. [3]

Solution

- product must not be initialized to zero.

- number is no initialised before the while loop.

- print product – must be outside the loop body.

(b) Write a program which inputs 10 numbers, multiplies them together and finally outputs
the answer (the product). [7]
Module Module1

Sub Main()
Dim count As Integer = 0
Dim product As Integer = 1
Dim number As Integer
While count < 5
Console.WriteLine("Enter number")
number = Console.ReadLine()
product = product * number
count = count + 1
End While
Console.WriteLine(product)
Console.ReadKey()
End Sub

End Module

Output
28
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

Trace table for the code above.

Count Number Product Output


0 1
1 2 2
2 3 6
3 4 24
4 5 120
5 6 720 720

NB. To show the results of the trace table in the code, the lines
Console.WriteLine(product)
Console.ReadKey()
are inserted inside While …….. EndWhile loop.
Nb. Press enter key twice for results.

NB. Modification of the code to produce results as above.


Module Module1
29
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

Sub Main()
Dim count As Integer = 0
Dim product As Double = 1
Dim number As String
While count < 5
Console.WriteLine("Enter number")
number = Console.ReadLine()
product = product * number
count = count + 1
Console.WriteLine(product)
Console.ReadKey()
End While
End Sub
End Module
Q. State any three differences between Repeat … Until and While … EndWhile loop
constructs / structures.

25. The following algorithm contains an error.


1. SET X = 1
2. REPEAT
3. X = X + 2
4. Print X
5. UNTIL X = 10
(a) Trace the algorithm and explain what the error is. [2]

Solution
trace – 3,5,7,9,11…… [1]
reason – x is odd / loop does not terminate / goes on forever [1]

(b) What is the purpose of the algorithm? [1]

Solution
To print odd numbers starting from 3. [1]

(b). Write a code which outputs the algorithm in (a). [6]


NB. The line Loop Until x >= 10 is modified from Loop Until x = 10 to just
show the first outputs.
30
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

Module Module1

Sub Main()
Dim x As Integer = 1
Do
x = x + 2
Console.WriteLine(x)

Loop Until x >= 10 'Add zero to 10 to show larger


output

Console.ReadKey()

End Sub

End Module
Output

26(a) Study the following algorithm:


1 x=1
2 Repeat
3 A=x*x
4 Output X, A
5 X=x+1
6 Until x = 3
7 End
Using a trace table, dry run the algorithm. [8]
Solution

Trace Table

Line X A Output
1 1
3 1 1
4 1 1 1,1
31
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

5 2 1
6 2 1
3 2 4
4 2 4 2,4
5 3

NB. Was there any error in the Algorithm?


- It’s not always that if the output is inside the loop there is an error.

Code

Module Module1

Sub Main()
Dim x As Integer = 1
Dim A As Integer
Do
A = x * x
Console.WriteLine(x & "," & A) '& display a character
or nothing.
'the above line enables the program to display value of X and A on the same line
with a comma separating them.
x = x + 1
Loop Until x = 3
Console.ReadKey()

End Sub

End Module

Screen Shot
32
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

27. A prime number is a number that can only be divided by the number itself and 1. Write a
program that displays all the prime numbers between 2 and 1000. [10]
Module Module1

Sub Main()
Dim i, j As Integer
Console.WriteLine("Prime numbers from 1 to 1000")
For i = 2 To 100
For j = 2 To 100
If ((i Mod j) = 0) Then
Exit For
End If
Next
If (j > (i / j)) Then
Console.WriteLine("{0} is prime number", i)
'{0} stores the value & is prime number - is optional
End If
Next
Console.WriteLine("{0} is a prime number", i)
Console.ReadKey()
End Sub
33
PROGARMMING QUESTIONS AND SUGGESTED ANSWERS IN CONSOLE APPLICATION

End Module

You might also like