Solutions

PALESTINE POLYTECHNIC UNIVERSITY
COLLEGE OF ADMINISTRATIVE SCIENCES AND INFORMATICS
MULTIMEDIA /GRAPHIC DEPARTMENT
4542 –Visual Programming
Second Exam
Abdalfatah Najjar
[email protected]
Second Semester 2013-2014
Student name:………………………...…
Date: Wednesday, May 07, 2014
Student number: …………….……….….
Time duration: 60 minutes
NOTES:
Please read these instructions carefully before you start.
Exam Rules:
‫اكتب اسمك الثالثي باللغة العربية‬
The duration of the exam is strictly 60 minutes. No extra time will be given
This exam is closed book and closed notes.
No books, notes, calculators, cellular phones, PDAs, or other electronic devices are permitted.
Show your work to earn partial credit.
Feel free to use the backs of the sheets for additional work space. However, you must mark clearly
where we need to look on the back.
 You must write clearly! Answers which are illegible will receive no credit.
 Programming problems will be marked based on both correctness and efficiency.






Hints:
 Work quickly and efficiently.
 You don't have to fill all the space that is given.
 Please use a pen for all answers.
Any CHEATING will result in an F as well as being written-up on academic dishonesty.
ATTENTION: Make sure your exam has 5 pages and all 3 questions.
Question #
Points Available
Points Earned
Q1
20
Q2
10
Q3
10
Total Grade
40/2
You got_______________out of 20 points.
Solutions
Page 1/5
[Question#20] Guess the Number Application (10 points)
Develop an application that generates a random numbers and prompts the user to guesses the number .when
the user clicks the New Game button, the application chooses a number in the range 1 to 100 at random. The
user enters guesses into the Guess: Text Box and clicks the Enter Button. If the guess is correct, the game is
ends, and the user can start a new game .If the guess is not correct, the application should indicate whether
the guess is higher or lower than the correct number.
Public Class GuessNumberForm
Dim randomObject As New Random( )
Dim number As Integer = randomObject.Next(1, 101)
' handles Enter button click event
Private Sub enterButton_Click(sender As System.Object,e As System.EventArgs) Handles enterButton.Click
' retrieve the user's guess
Dim guess As Integer = Convert.ToInt32(guessTextBox.Text)
' check answer
If guess = number Then
outputLabel.Text = "Correct!"
enterButton.Enabled = False
newGameButton.Enabled = True
ElseIf guess > number Then
outputLabel.Text = "Too high..."
Else
outputLabel.Text = "Too low..."
End If
guessTextBox.Focus() ' give focus to the TextBox
End Sub ' enterButton_Click
Page 2/5
' handles New Game button click event
Private Sub newGameButton_Click(sender As System.Object,e As System.EventArgs) Handles newGameButton.Click
' start a new game
number = randomObject.Next(1, 101) ' generate new number
enterButton.Enabled = True ' enable the Enter Button
newGameButton.Enabled = False ' disable the New Game Button
outputLabel.Text = String.Empty ' clear result
guessTextBox.Clear( ) ' clear the previous guess
guessTextBox.Focus( )
End Sub ' newGameButton_Click
' handles Guess TextBox's TextChanged event
Private Sub guessTextBox_TextChanged(sender As System.Object,e As System.EventArgs) Handles
guessTextBox.TextChanged
outputLabel.Text = String.Empty ' clear result
End Sub ' guessTextBox_TextChanged
End Class ' GuessNumberForm
[Question#2] Output (10 points)
What is the result of the following code?
A.
Dim y As Integer
Dim x As Integer
Dim mysteryValue As Integer
x=1
mysteryValue = 0
Do
y=x^2
displayListBox.Items.Add(y)
mysteryValue += 1
x += 1
Loop While x <= 10
resultLabel.Text = mysteryValue
Answer: The value displayed in
resultLabel is 10
and the ListBox will contain
1, 4, 9, 16, 25, 36, 49, 64, 81, 100.
B.
Assume that power, result and number are all declared as Integers.
power = 5
number = 10
result = number
For i As Integer = 1 To (power - 1)
result *= number
Next
Page 3/5
Answer: This code segment raises
number to the power. In this case,
result gets 105
(100000).
[Question#3] Tough Choices (10 points)
For each of the following questions, circle one answer out of the choices given.
1. Which of the following statements will assign a random number between 1 and 50 inclusive to intNum?
A. intNum = rand.Next(50) + 1
B. intNum = rand.Next(1, 50)
C. intNum = rand.Next(50)
D. intNum = rand.Next( )
2. Which statement about the ListBox.Add method is true?
A.
B.
C.
D.
It will always place the item at the end of the list.
It can be used to place an item anywhere in the list.
It finds the sum of all of the items in the list.
It always puts the item at the beginning of the list
3. A ToolTip is a ________________ that allows the programmer to create a small popup message that
displays when the user places the mouse over a control.
A. method
B. property
C. function
D. control
4. A procedure may not be accessed by procedures from another class or form if the ____________ access
specifier is used.
A. Private
B. Public
C. Static
D. Scope
5. Which of the following procedures will keep track of the number of times it was called?
A. Private Sub KeepTrack(ByVal intCount As Integer)
intCount += 1
End Sub
B. Private Sub KeepTrack()
Dim intCount As Integer
intCount += 1
End Sub
C. Private Sub KeepTrack()
Static intCount As Integer
intCount += 1
End Sub
D. Private Sub KeepTrack()
Public intCount As Integer
intCount += 1
End Sub
Page 4/5
6. Which of the following statements most accurately describes the Me keyword?
A.
B.
C.
D.
The Me keyword refers to the current instance of the form.
Me is another name for the Form Load event procedure.
The Me.Close( ) statement calls the Close method of a form named Me.
The Me keyword provides access to the controls of a form without naming the specific control.
7. A procedure defined with keyword Sub .
A.
B.
C.
D.
must specify a return type
does not accept arguments
returns a value
does not return a value
8. What is the difference between Sub and Function procedures?
A.
B.
C.
D.
Sub procedures return values, Function procedures do not.
Function procedures return values, Sub procedures do not.
Sub procedures accept parameters, Function procedures do not.
Function procedures accept parameters, Sub procedures do not.
9. The statement executes at least once and continues executing until its loop termination condition
becomes true.
A. Do While…Loop
B. Do…Loop Until
C. Do…Loop While
D. Do Until…Loop
10.The statement assigns value a random number in the range 5–20.
A.
B.
C.
D.
value = randomObject.Next(5, 21)
value = randomObject.Next(4, 20)
value = randomObject.Next(5, 20)
value = randomObject.Next(4, 21)
Good luck
Course instructors: Abdalfatah Najjar
Page 5/5