SFDV3001 Data Structures Summer, 2011 Java Review Questions 1

advertisement
SFDV3001 Data Structures
Summer, 2011
Java Review Questions
1- Write a program that reads a sequence of positive integers. The number of integers to
be entered should be specified by the user. Calculate the average of the numbers.
Remember, the average of a collection of numbers is: (their sum)/(how many numbers
there are).
average = sum/n
where sum is the addition of all numbers, and n is how many numbers there are
While reading input, if the user enters a negative number, ask him/her to re-enter it again.
2- Write a program that asks the user to enter a sentence then pass it to a method called
“reverse”. The method “reverse” doesn’t return anything (i.e type void); it only prints the
sentence to the screen reversed. Meaning, if the user enters the sentence: “Hello there!”
the output should be “!ereht olleH”
3- Write an application program that asks the user to enter 10 elements of type double.
The values entered must be in the range [0.0, 100.0], in case of invalid input, you should
ask the user to enter the number again
4- Create a two-dimensional array named “zeros” with 2 rows and 4 columns. The
elements of the array are of type integer. The user should be asked to initialize all the
elements of the array. Then write a code segment that counts how many zeros are in this
array and print out their exact position. For example, if the element at position [1][2] is
equal to zero, then it should be counted and the program should print: “0 found at row 1,
column 2”. In the end, print to the screen the total number of zeros found.
5- Write an application program that asks the user to enter three different elements of
type doubles. Print to the screen the element in the middle. So for example, if the user
enters: 9.0,2.5,11.0, then the output should be 9.0 because 2.5<9.0<11.0
6- Write a program that asks the user to input an integer for which we want to display the
multiplication table. For example, if the user enters 3, then the output should be:
1 2 3
2 4 6
3 6 9
SFDV3001 Data Structures
Summer, 2011
Java Review Questions
The multiplication table must be produced by a method named “multiply” which takes
one parameter of type integer, which is the number entered by the user.
7- Write a program to compute FACTORIAL of a number ‘n’. The number for which
factorial has to be computed should be specified by the user. (For example: if the number
specified by the user is 4, the expected result is 4x3x2x1=24).
8-Write a program that uses the conditional control statement SWITCH option to choose
one operation among the following every time when you execute (run) the program:
a. When user presses number key 1, accept two numbers and perform
addition of them.
b. When user presses number key 2, accept two numbers and perform
subtraction of them
c. When user presses number key 3, accept two numbers and perform
multiplication of them
d. When user presses any other key, print a error message (‘Improper
Choice!’) and quit.
9-Write a program that displays number from 11 to 100, 10 values per line. See the
typical answer as below:
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30.
………………………………………
………………………………………
10- A palindrome is a sentence (or a word) which, if read, from both directions will have
the same meaning. For example, “bob” is considered a palindrome. Also “rise to vote sir”
is a palindrome. There are many other palindromes that you can look up on the internet.
Your task is to write a program that will allow the user to enter a word or sentence and
find out whether it is a palindrome or not. It is important to note that when you check
whether an input is a palindrome, you should ignore all white spaces and non-letter
characters. So in the example above: “rise to vote sir” is a palindrome only when you
ignore all spaces. Your program should identify all palindromes regardless of their
length.
Other examples of palindromes are:
“race car”, “Do geese see God”, “Some men interpret nine memos”
Download