Review for Exam 2

Download Report

Transcript Review for Exam 2

Review for Exam 2
Teams – Section B
•
•
•
•
•
•
Chris G, Nikki, Jen, Angie, Spencer
Katie, Isaac, Kevin, Lexi, Anders
Stephanie, Ryan, Eddie, Matt, Browning
Tyler W, Trey, Lara, Chris S, John
Dylan, Julian, Harrison, Megan
Sarah, Tyler Y, Draper, Joanna, Peter
Teams – Section C
•
•
•
•
•
•
Stephanie, Matt, Nick, Billy, Wes
Evan, Claire, Chris, John V, Nicole
Tatum, Ellie, Jade, Aaron, Charles
Steven, Collin, Jon H, Christy, Scott
Christina, Tyler, William, Tommy, Drew
Katy, Michael, Tim, Olivia, Pete
1.
• What is the value of x?
String question = “Exam 2”;
int x = question.indexOf(“x”);
2.
• What is the value of x?
String question = “Exam 2”;
char x = question.charAt(4);
3.
• What is the value of x?
String question = “Exam 2”;
int x = question.length();
4.
• What is the value of x?
String question = “Exam 2”;
String x = question.substring(0);
5.
• What is the value of x?
String question = “Exam 2”;
int x = question.indexOf(“e”);
6.
What does it print?
String name = “CSC130”;
for(int i = name.length()-1; i>=0; i--){
System.out.println(name.charAt(i));
}
7.
• What does this code print?
String s = “Happy”;
int i = 0;
while(i<s.length()){
System.out.println(s.charAt(i));
i = i + 2;
}
8.
• What does this code print?
int x = 4;
while(x>0){
System.out.println(x);
x = x -2;
}
9.
• What does this code print?
for(int i = 1; i<=4; i++){
System.out.println(“Hey”);
}
10.
• What is printed?
for(int x = 0; x < 10; x++){
System.out.println(x);
}
11.
• Find the error:
String s = “01234567”;
for(int i = 0; i<= s.length(); i++){
System.out.println(s.charAt(i));
}
12.
• Find the error:
String s = “01234567”;
for(int i = 0; i<= 5; i++){
System.out.println(s.charAt(i-1));
}
13.
Find the error:
String s = “01234567”;
for(int i = 0; i<s.length(); i++){
String part = s.substring(i,i+2);
System.out.println(part);
}
14.
What line of code declares and creates
an array of 50 ints? (Call it whatever
you want.)
15.
• What is the error?
int[] numbers;
int n = numbers[1];
16.
• How do you find the last number in an
array called values?
17.
• Recite the magic for loop for an array
called group.
18.
• What does this code do?
int[] numbers = new int[100];
19.
• What is the value of x?
int[] values = {1,2,3};
int x = 0;
for(int i=0; i<values.length; i++){
x = x+values[i];
}
20.
• What is the error?
String[] words = new String[100];
System.out.println(words[0].length());
• What is the error?
21.
int[] numbers = {1,2,3};
int x = numbers[3];
• What does it do?
22.
public int mystery(int[] input){
int answer = 0;
for(int i = 0; i< input.length; i++){
if (input[i] > 0){
answer++;
}
}
return answer;
}
23.
public class Game{
public int[] getX(double radius){
…
}
public void draw(){
// What line of code goes here to call the
method?
}
24.
public class Game{
public void printArray(double[] radius){
…
}
public void draw(){
double[] nums = {1.5, 3.0};
// What line of code goes here to call the method?
}
25.
• Find the error:
public int firstZeroIndex(int[] input){
for(int i = 0; i< input.length; i++){
if (input[i] == 0){
return i;
}
else{
return -1;
}
}
return -1;
}
26.
Fill in the blank with a type, or with X to
say that no type will make the line
compile:
Bear: has getName() method
Bear[] bears = …
____ a = bears[0];
27.
Fill in the blank with a type, or with X to
say that no type will make the line
compile:
Bear: has getName() method
Bear[] bears = …
____ b = bears.getName();
28.
Fill in the blank with a type, or with X to
say that no type will make the line
compile:
Bear: has getName() method
Bear[] bears = …
____ c = bears[0].length;
29.
Scanner input = new Scanner(System.in);
What line of code reads a double from the
keyboard and calls it “decimal”?
30.
What is the difference between the way
you call JOptionPane’s showInputDialog
and showMessageDialog?
All-Play
A car is racing down the highway, starting
at 40 mph. Each minute that goes by it
adds 10% to its speed. How fast is the
car going after a given number of
minutes?
public double speed(int numMins)
public double speed(int numMins){
double speed = 40;
int minute = 0;
while(minute < numMins){
speed = speed + (.10 * speed);
minute++;
}
return speed;
}
Ask the user for an integer at the
Console. Print a String “bar graph” with
that number. The bar graph should be
that number of X’s then a space and the
number. A bar graph for 4 would look like
this:
XXXX 4
public void makeBar(){
public void makeBar(){
Scanner scan = new
Scanner(System.in);
System.out.println(“Enter a number”);
int num = scan.nextInt();
for(int i = 1; i<=num; i++){
System.out.print(“X”);
}
System.out.println(“ “+num);
}
• Given a word and a character, return
how many times that character appears
in the word.
• public int count(String phrase, char let)
public int count(String phrase, char let){
int count = 0;
for(int i = 0; i< phrase.length(); i++){
char letter = phrase.charAt(i);
if (letter == let){
count++ ;
}
}
return count;
}
Given an array of Colors, find the Color closest
to a gray value. Gray values are colors with the
same R, G, and B components. To find how
close to gray a Color is, use this line:
int value = Math.abs(red-green)+Math.abs(green-blue)
+Math.abs(red-blue);
You are looking for the Color whose value is
closest to 0.
public Color closestToGray(Color[] colors){
public Color closestToGray(Color[] colors){
Color closest = colors[0];
int smallest = Integer.MAX_VALUE;
for(int i =0; i< colors.length; i++){
Color current = colors[i];
int red = current.getRed();
int green = current.getGreen();
int blue = current.getBlue();
int value = Math.abs(red-green)+Math.abs(green-blue)
+Math.abs(red-blue);
if(value < smallest){
smallest = value;
closest = current;
}
}
return closest;
}
int x = 0;
int y = 3;
System.out.println(“Starting”);
for(int i = 1; i<= y; i++){
System.out.println(i);
x++;
y--;
}
int z = 2;
while(z>=0){
if (z%2==0){
System.out.println(“Even”);
}
System.out.println(“Hey”);
z= z-1;
}
What is printed?
int x = 5;
int y = 1;
int z = 0;
while(x>y){
System.out.println(“x is bigger.”);
if (x-2>y){
System.out.println(“x is way bigger.”);
z++;
}
y++;
}
System.out.println(x+”, ”+y+”, “+z);
What is printed?
int x = 3;
int y = 1;
int z = 0;
for(int i = 1; i<4; i++){
System.out.println(i);
while (x>y){
System.out.println(“In while”);
z++;
y++;
}
y--;
}
System.out.println(x+”, ”+y+”, “+z);
What is printed?