Sunteți pe pagina 1din 7

Joanna Klukowska

joannakl@cs.nyu.edu

CSCI-UA 101
Practice Questions for Midterm 1

Practice Questions for Midterm 1:


Basic Java, Flow Control, Looping, Methods, 1D Arrays
1. Write a program that reads integers from the keyboard until a user enters zero. Have the program
print
the sum of all the numbers entered,
the largest number entered,
the smallest number entered.
2. Show the for statement for a loop that counts from 1000 to 0 by 2.
3. What does the following code fragment print?

for(int i = 0; i<10; i++) {


System.out.print(i + " ");
if((i%2) == 0) continue;
System.out.println();
}
4. What is an infinite loop?
5. Show how a short-circuit AND operator (&&) can be used to prevent a divide-by-zero error.
6. Write a program that finds all of the prime numbers between 2 and 100. (Write the entire program.)
7. Given this output,
One
Two
Three
show the (single) println( ) statement that produced it.
8. Rewrite the following code as indicated:
(a) write it using a for loop instead of the while loop

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

// Find interest rate based on year


if (numOfYears == 7)
annualInterestRate = 7.25;
else if (numOfYears == 15)
annualInterestRate = 8.50;
else if (numOfYears == 30)
annualInterestRate = 9.0;
else
System.out.println("Wrong number of years");
9. Answer the following multiple choice questions (there might be more than one correct answer):
(a) When you invoke a method with a parameter, the value of the argument is passed to the parameter. This is referred to as _________.
i.
ii.
iii.
iv.

pass by name
pass by value
pass by reference
method invocation

(b) Analyze the following code.

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:

int[] list = new int[10];


for (int i = 0; i <= list.length; i++) {
list[i] = (int)(Math.random() * 10);
}
Which of the following statements is true?
i.
ii.
iii.
iv.

list.length must be replaced by 10


The loop body will execute 10 times, filling up the array with random numbers.
The loop body will execute 10 times, filling up the array with zeros.
The code has a runtime error indicating that the array is out of bound.

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

(e) What is the value of x after the following statements?

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

(g) A variable defined inside a method is referred to as __________.


i.
ii.
iii.
iv.

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

(i) What is the result of 45 / 4?


i. 11
ii. 12
3

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

int lower = 3, upper = 7, sum = 0, i;


for (i = lower; i <= upper; i++) {
sum = sum + i;
System.out.print( sum + " ");
}
13. Identify and correct the error(s) and/or problems, if any, in each of the following code fragments
(assume that they are embedded in otherwise correct Java programs).
(a)
1 int answer = 1;
2 Scanner input = new Scanner ( System . in );
3 while ( answer = 1 ) {
4
System . out . print ( " Do you want to play again ? \ n" +
5
" [ type 1 for yes , and 0 for no ]\ n " );
6
answer = input . nextInt ();
7}
8 System . out . println (" Thank you for playing ! " );

(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.

public class Test {


public static void main(String[] args) {
int number = 0;
int[] numbers = new int[1];
numbers[0] = 0;
m(number, numbers);
System.out.println("number is " + number
+ " and numbers[0] is " + numbers[0]);
}
public static void m(int x, int[] y) {
x = 3;
y[0] = 3;
}
}
24. Write the following method that returns true if the list is already sorted in increasing order and
false if it is not sorted.

public static boolean isSorted(int[] list)


25. Write a method that given a String s as a parameter, computes and returns the reverse of s.
26. Write a method that given a list of floating point numbers determines if a particular value is on the
list. Your method should return the location of the item if it is found, or -1 if it is not found. Use the
following method header:

public static int find(double [] list, double key)


Use the searching algorithm of your choice.

S-ar putea să vă placă și