Intro to Computer Science 1 Final Study Guide

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

A 2-dimensional array of ints, has been created and assigned to a2d. Write an expression whose value is the number of elements in the first row. (Assume the array is not empty.)

a2d[0].length

Given that an array named a whose elements are of type int has been declared, assign the value -1 to the last element in a.

a[a.length - 1] = -1;

Given the String variable address, write an expression that returns the position of the first occurrence of the String "Avenue" in address.

address.indexOf("Avenue")

Given an int variable k that has already been declared, use a for loop to print a single line consisting of 97 asterisks. Use no variables other than k.

for (k = 0; k < 97; k++) { System.out.print("*"); }

You have a variable, n, with a non-negative value, and need to write a loop that will keep print n blank lines. What loop construct should you use?

for loop

Given two integer variables matricAge and gradAge, write a statement that gives gradAge a value that is 4 more than the value of matricAge.

gradAge = matricAge; gradAge += 4;

Write a statement that toggles the value of onOffSwitch. That is, if onOffSwitch is false, its value is changed to true; if onOffSwitch is true, its value is changed to false.

if (!(onOffSwitch)) { onOffSwitch = true; } else { onOffSwitch = false; }

Write an if/else statement that compares the variable age with 65, adds 1 to the variable seniorCitizens if age is greater than or equal to 65, and adds 1 to the variable nonSeniors otherwise.

if (age >= 65) seniorCitizens ++; else nonSeniors ++;

Write a conditional that decreases the variable shelfLife by 4 if the variable outsideTemperature is greater than 90.

if (outsideTemperature > 90) { shelfLife -= 4; }

Clunker Motors Inc. is recalling all vehicles from model years 1995-1998 and 2004-2006. Given a variable modelYear write a statement that prints the message "RECALL" to standard output if the value of modelYear falls within those two ranges.

if((modelYear > 1994 && modelYear < 1999 )||(modelYear > 2003 && modelYear < 2007 )) { System.out.print("RECALL"); } else { }

Online Book Merchants offers premium customers 1 free book with every purchase of 5 or more books and offers 2 free books with every purchase of 8 or more books. It offers regular customers 1 free book with every purchase of 7 or more books, and offers 2 free books with every purchase of 12 or more books. Write a statement that assigns freeBooks the appropriate value based on the values of the boolean variable isPremiumCustomer and the int variable nbooksPurchased.

if(nbooksPurchased > 4) { if(isPremiumCustomer) { freeBooks = 1; if(nbooksPurchased > 7) { freeBooks = 2; } }else { freeBooks = 0; if(nbooksPurchased > 6) { freeBooks = 1; } if(nbooksPurchased > 11) { freeBooks = 2; } } }else{freeBooks = 0; }

Write an expression that evaluates to true if the value of index is greater than the value of lastIndex.

index > lastIndex

Write the definition of a method named sumArray that has one parameter, an array of ints. The method returns the sum of the elements of the array as an int.

int sumArray(int a[]) { int i, total=0; for(i = 0; i < a.length; i++) { total += a[i]; } return total; }

Declare and instantiate an array named scores of twenty-five elements of type int.

int[] scores = new int[25];

Declare a two-dimensional array of integers named tictactoe.

int[][] tictactoe;

Given an int variable k that has already been declared, use a do...while loop to print a single line consisting of 53 asterisks. Use no variables other than k.

k = 0; do { System.out.print("*"); k ++; } while(k < 53);

Given an int variable k that has already been declared, use a while loop to print a single line consisting of 88 asterisks. Use no variables other than k.

k=1; while(k <= 88) { System.out.print('*'); k++; } System.out.println();

Assume there is a class AirConditioner that supports the following behaviors: turning the air conditioner on and off, and setting the desired temperature. The following methods provide these behaviors: turnOn and turnOff, which accept no arguments and return no value, and setTemp, which accepts an int argument and returns no value. Assume there is a reference variable officeAC of type AirConditioner. Create a new object of type AirConditioner and save the reference in officeAC. After that, use the reference to turn on the new air conditioner object and set the desired temperature to 69 degrees.

officeAC = new AirConditioner(); officeAC.turnOn(); officeAC.setTemp(69);

Write a sequence of statements that finds the first comma in the String line, and assigns to the variable clause the portion of line up to, but not including the comma. You may assume that an int variable pos, as well as the variables line and clause, have already been declared.

pos = line.indexOf(","); clause = line.substring(0, pos);

printArray is a method that accepts one argument, an array of ints. The method prints the contents of the array; it does not return a value. inventory is an array of ints that has been already declared and filled with values. Write a statement that prints the contents of the array inventory by calling the method printArray.

printArray(inventory);

Given an integer variable profits, write a statement that increases the value of that variable by a factor of 10.

profits *= 10;

You are given a class named Clock that has one int instance variable called hours. Write a constructor with no parameters for the class Clock. The constructor should set hours to 12.

public Clock() { hours = 12; }

Write a class named Acc2 containing: An instance variable named sum of type integer, initialized to 0. A method named getSum that returns the value of sum.

public class Acc2 { private int sum = 0; public int getSum() { return sum; } }

Write the definition of a class Clock. The class has no constructors and two instance variables. One is of type int called hours and the other is of type boolean called isTicking.

public class Clock { private int hours; private boolean isTicking; }

Write the definition of a method powerTo, which receives two parameters. The first is a double and the second is an int. The method returns a double. If the second parameter is negative, the method returns zero. Otherwise it returns the value of the first parameter raised to the power of the second parameter.

public double powerTo (double x, int y) { if (y < 0) { return 0.0; } else { return Math.pow(x, (double) y); } }

Write the definition of a method printArray, which has one parameter, an array of ints. The method does not return a value. The method prints out each element of the array, on a line by itself, in the order the elements appear in the array, and does not print anything else.

public static void printArray(int[] a) { for (int i = 0; i < a.length; i++) { System.out.print(a[i]); System.out.println(); } }

Write the definition of a method printGrade, which has a char parameter and returns nothing. The method prints on a line by itself the message string Grade: followed by the char parameter (printed as a character) to standard output. Don't forget to put a new line character at the end of your line.

public static void printGrade(char x) { System.out.println("Grade: " + x); }

Given a String variable named sentence that has been initialized, write an expression whose value is the number of characters in the String referred to by sentence.

sentence.length()

Write a switch statement that tests the value of the char variable response and performs the following actions: if response is y, the message Your request is being processed is printed if response is n, the message Thank you anyway for your consideration is printed if response is h, the message Sorry, no help is currently available is printed for any other value of response, the message Invalid entry; please try again is printed

switch (response) { case 'y': System.out.print("Your request is being processed"); break; case 'n': System.out.print("Thank you anyway for your consideration"); break; case 'h': System.out.print("Sorry, no help is currently available"); break; default: System.out.print("Invalid entry; please try again"); break; }

Given three already declared int variables, i, j, and temp, write some code that swaps the values in i and j. Use temp to hold the value of i and then assign j's value to i. The original value of i, which was saved in temp, can now be assigned to j.

temp = i; i = j; j = temp;

Assume that tictactoe has been declared to be a two-dimensional array of integers. Write a statement that instantiates a 3x3 two-dimensional array of integers and assign it to tictactoe.

tictactoe = new int[3][3];

A 2-dimensional 3x3 array of ints, has been created and assigned to tictactoe. Write an expression whose value is true if the elements of the first column are all equal.

tictactoe[0][0] == tictactoe[0][0] && tictactoe[0][0] == tictactoe [2][0]

A 2-dimensional 3x3 array of ints, has been created and assigned to tictactoe. Write an expression whose value is true if the elements of the first row are all equal.

tictactoe[0][1] == tictactoe[0][1] && tictactoe[0][0] == tictactoe[0][2]

A 2-dimensional 3x3 array of ints, has been created and assigned to tictactoe. Write an expression whose value is true if the elements of the last row are all equal

tictactoe[2][0] == tictactoe[2][0] && tictactoe[2][0] == tictactoe[2][2]

Given int variables k and total that have already been declared, use a while loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total. Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.

total=0; for (k=1;k<=50; k++) total+=k*k;

Write a statement that reads an integer value from standard input into val. Assume that val has already been declared as an int variable. Assume also that stdin is a variable that references a Scanner object associated with standard input.

val = stdin.nextInt();

You need to write a loop that reads integers and adds them to a sum as long as they are positive. Once 0 or a negative value is read in your loop terminates. Which is the preferred loop construct to use?

while loop

Assume that word is a variable of type String that has been assigned a value. Write an expression whose value is a String consisting of the first three characters of the value of word. So if the value of word were "dystopia" the expression's value would be "dys".

word.substring(0, 3)

Assume that word is a variable of type String that has been assigned a value. Write an expression whose value is a String consisting of the last three characters of the value of word. So if the value of word were "biggest" the expression's value would be "est".

word.substring(word.length() - 3)

Write an expression that evaluates to true if the integer variable x contains an even value, and false if it contains an odd value.

x % 2 == 0

Given a String variable named sentence that has been initialized, write an expression whose value is the index of the very last character in the String referred to by sentence.

(sentence.length() - 1)

Given the variables temperature and humidity, write an expression that evaluates to true if and only if the temperature is greater than 90 and the humidity is less than 10.

(temperature > 90) && (humidity < 10)


Set pelajaran terkait

The Art of Creating Suspense: Central Ideas of Two Authors

View Set

Smartbook Chapter 7: Trust, Justice, and Ethics

View Set

Gardner's Art Through the Ages, 14e Chapter 28 Impressionism, Post-Impressionism, Symbolism: Europe and America, 1870 to 1900

View Set