Documente Academic
Documente Profesional
Documente Cultură
joannakl@cs.nyu.edu
CSCI-UA 101
Practice Questions for Midterm 1
int i = 1;
while (i <= 10 ) {
if ( i < 5 && i != 2 )
System.out.println("X");
i++;
}
(b) write it using a while loop instead of the for loop
1
Joanna Klukowska
joannakl@cs.nyu.edu
CSCI-UA 101
Practice Questions for Midterm 1
int minutes;
for ( minutes = 10; minutes > 0 ; minutes-- ) {
System.out.println("You have " + minutes + " minutes left");
(c) Convert the following if statement to a switch statement
pass by name
pass by value
pass by reference
method invocation
int x = 1;
while ( x > 0 && x < 100 )
System.out.println(x++);
i. The loop runs forever.
ii. The code does not compile because the loop body is not in the braces.
iii. The code does not compile because ( 0 < x && x < 100 ) does not use parentheses
properly.
iv. The numbers 1 to 99 are displayed.
v. The numbers 2 to 100 are displayed.
(c) Consider the following code fragment:
Joanna Klukowska
joannakl@cs.nyu.edu
CSCI-UA 101
Practice Questions for Midterm 1
(d) What is the value in count after the following loop is executed?
int count = 0;
do {
System.out.println("Welcome to Java");
} while (count++ < 9);
System.out.println(count);
i.
ii.
iii.
iv.
v.
11
0
9
8
10
float x;
x = 15/4;
i.
ii.
iii.
iv.
3.75
4.0
3.0
60
(f) Which of the following expression yields an integer between 0 and 100, inclusive?
i.
ii.
iii.
iv.
v.
(int)(Math.random()
(int)(Math.random()
(int)(Math.random()
(int)(Math.random()
(int)(Math.random()
*
*
*
*
*
100 + 1)
101)
100)
100) + 1
101) + 1
a local variable
a block variable
a global variable
a method variable
(h) Each time a method is invoked, the system stores parameters and local variables in an area of
memory, known as _______, which stores elements in last-in first-out fashion.
i.
ii.
iii.
iv.
storage area
a heap
a stack
an array
Joanna Klukowska
joannakl@cs.nyu.edu
CSCI-UA 101
Practice Questions for Midterm 1
iii. 10
iv. 11.25
10. Write a complete Java program that prompts the user to enter an integer. If the number is a multiple
of 5, print HiFive. If the number is divisible by 2 or 3, print New York, otherwise do not print
anything.
Here are the sample runs:
Enter an integer:
New York
Enter an integer:
HiFive New York
15
Enter an integer:
HiFive
25
Enter an integer:
17
11. Write a loop that computes the following sum. (No need to write a complete program)
100 99 98
3
2
1
+ + +...+ + +
1
2
3
98 99 100
12. What is the output of the following program lines when they are embedded in a correct Java program.
(a) Suppose the input is 2 3 4 5 0. What is the output of the following code?
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number, max;
number = input.nextInt();
max = number;
while (number != 0) {
number = input.nextInt();
if (number > max)
max = number;
}
System.out.println("max is " + max);
System.out.println("number " + number);
}
}
(b)
Joanna Klukowska
joannakl@cs.nyu.edu
CSCI-UA 101
Practice Questions for Midterm 1
(b)
1 f i n a l i n t NUMBER_OF_ROLLS = 1 0 0 0 0 ;
2 int
face ;
3 f o r ( i n t r o l l = 1 , r o l l <= NUMBER_OF_ROLLS,
4{
5
/ / random number from 1 to 6
6
f a c e = 1 + ( i n t ) ( Math . random ( ) 6 ) ;
7}
(c)
1 public class Test {
2
p u b l i c s t a t i c v o i d method ( i n t x ) {
3
/ / do s o m e t h i n g h e r e
4
}
5
p u b l i c s t a t i c v o i d method ( i n t y ) {
6
/ / do s o m e t h i n g e l s e h e r e
7
}
8}
r o l l ++ )
Joanna Klukowska
joannakl@cs.nyu.edu
CSCI-UA 101
Practice Questions for Midterm 1
14. Write a program that prompts the user to enter an integer n (assume n >= 2) and displays its largest
factor other than itself.
15. Write a method that given an integer determines if it is even (returns true if it is, false otherwise).
Make sure to use meaningful names and document the method.
16. Write a method that given a positive integer computes the sum of its digits. Your method should
verify that the parameter passed to it is a positive number.
HINT: you will need to use % operator. For example to extract the last digit of 6582, you can use
6582%10 to obtain 2.
17. Write a method that given three real numbers, prints them to standard output from smallest to largest.
18. Write a method that computes the area of a regular pentagon given the length of its side. The formula
for the area of a pentagon is
Area =
5 s2
.
4 tan (/5)
19. What is the output of the following program lines when they are embedded in a correct program and
i is of type int and initialized as in the options listed below?
1 i n t i = ______ ; / / l i n e t o
2
3 switch ( i )
4{
5
c a s e 0 : i = 1 5 ; break ;
6
c a s e 1 : i = 5 i ;
7
c a s e 2 : i = i ; break ;
8
case 3: i = 40;
9
d e f a u l t : i = 0 ; break ;
10 }
11
12 System . o u t . p r i n t l n ( i ) ;
be
replaced
(a) int i = 0;
(b) int i = 1;
(c) int i = 3;
(d) int i = 4;
20. Write a method that given an integer n , returns a random number between n and n.
21. Write a method that, given two sorted arrays of integers, merges the two arrays into a single sorted
array that is returned.
22. Write a method that, given an array of integers, computes the sum of every other number (starting
at the zeroth index) and returns true if the sum is digisible by 10 and false otherwise.
Joanna Klukowska
joannakl@cs.nyu.edu
CSCI-UA 101
Practice Questions for Midterm 1
23. What is the output of the following program lines when they are embedded in a correct Java program.