l3 - z after executing the following code: int z = 0; for...

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

View Full Document Right Arrow Icon
CS121 Autumn 2009 Lecture #3 exercises 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
Background image of page 1
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 Javas 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 Javas 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

This note was uploaded on 10/10/2009 for the course CMSC 12100 taught by Professor Annerogers during the Fall '09 term at UChicago.

Ask a homework question - tutors are online