{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# l3 - z after executing the following code int z = 0 for(int...

This preview shows page 1. Sign up to view the full content.

CS121 Autumn 2009 Lecture #3 exercises Reading: Sedgewick&Wayne 1.3 The purpose of these exercises is to allow you to test your understanding of the material covered in today’s lecture. You are welcome to turn in your answers at the next lecture to get feedback from us, but you are not required to do so. These exercises are intended to take less than an hour, assuming you have done the reading. If you cannot figure out the answer to questions 1-3 in five or ten minutes, please ask for help. Exercise 1: What is the value of the variable x after executing the following loop: int x = 0; for (int i=2; i < 7; i++) { x = x + i * i; } Exercise 2: What is the value of the variable y after executing the following loop: int y = 0; for (int i=2; i < 7; i++) { if (i % 3 == 0) { y = y + i * i; } else { y++; } } Exercise 3: What is the value of the variable
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: z after executing the following code: int z = 0; for (int i=2; i < 7; i++) { for (int j = 0; j < i; j++) { z = z + j * j; } } Exercise 4: Write a program that takes an integer, k, as a command-line argument and prints a list of the powers of k that can be frepresented using Java’s int type. Your program should produce a suitable error message if k < = 0 . Test your code with different values of k. You can ﬁnd skeleton code for this assignment in /home/amr/cs121/L3 on the linux cluster. Hints: The expression Integer.MAX VALUE evaluates to the largest integer that can be represented with Java’s int type. You may ﬁnd the Java Math library: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Math.html useful....
View Full Document

{[ snackBarMessage ]}