Chapters 2-4 Test: Scanner and Methods
What is the output by the code below? System.out.print(12.7%3);
.7
What is the output by the code below? System.out.print( (double)2/2);
1.0
What is the output by the code below? double x = 4/3 System.out.print(x);
1.0
What is the output by the code shown below? double x = 9.0; x = x+2; System.out.print(x);
11.0
What is the output? System.out.print(3 + 3 * 3);
12
What is the output by the code shown below? int x = 9; x = x + 3 * 2; System.out.print(x);
15
What is the output by the code below? int x = 20; x = x*2; System.out.print(x);
40
What is the output by the code below? int x = 7; x++; System.out.print(x);
8
Which of the following would correctly define and instantiate a Scanner?
Scanner kb = new Scanner(in);
Given the following statement, var must be defined as which of the following types? var = keyboard.nextLine( );
String
What is a method?
a tool that helps organize a program
Given the following statement, var must be defined as which of the following types? var = keyboard.nextInt( );
int
Which of the following Scanner methods can be used to read in a String?
next( )
Which of the following Scanner methods can be used to read in a double?
nextDouble( )
Which of the following Scanner methods can be used to read in the value 52.245?
nextDouble( )
Which of the following Scanner methods can be used to read in the value comp sci is best?
nextLine( )
What is the output by the code below? public class CS { public void one( ) { System.out.print("one"); } public void two( ) { System.out.print("two"); one( ); } } //code in the main of another class CS test = new CS( ); test.one( ); test.two( ); test.one( );
onetwooneone
Which of the following Graphics methods can be used to draw a filled Circle?
fillOval( )
Which of the following Graphics methods can be used to draw a filled Rectangle?
fillRect( )
What is the output by the code below? int x = 9; int y = 8; int z = x+y; double a = z; System.out.print(a);
17.0
How many accessor methods are there in class It? public class It { private int myX; public It( ) { myX = 99; } public int getX( ) { return myX; } public String toString( ) { return "" + getX( ); } }
2
What is the output by the code below? char x = 'A'; System.out.print(x+3);
68
When writing a method, you must always have an open and close what?
A and B only
Which of the following Scanner methods can be used to read in the value compsciisbest?
A and B only next( ) and nextDouble( )
What is the output by the code below? System.out.println(Math.round(5/2));
2
What is the output by the code below? int x = 8; x = x%3; System.out.print(x);
2
How many methods are there in class It? public class It { private int myX; public It( ) { myX = 99; } public int getX( ) { return myX; } public String toString( ) { return "" + getX( ); } }
2
What is the output by the code below? System.out.println(Math.pow(3,3));
27.0
What is the output by the code below? System.out.println(Math.round(8.6));
9
What is the output by the code below? System.out.println(Math.sqrt(81));
9.0
What is the output by the code below? System.out.print(3/4);
0
What is the output by the code below? int x = 15; x = x % 5; System.out.print(x);
0
How many constructors are there in class It? public class It { private int myX; public It( ) { myX = 99; } public int getX( ) { return myX; } public String toString( ) { return "" + getX( ); } }
1
How many instance variables are there in class It? public class It { private int myX; public It( ) { myX = 99; } public String toString( ) { return "" + myX; } }
1
What is the output by the code below? System.out.print(4%3);
1
Which of the following Scanner methods can be used to read in the value 45?
all of these
Which of the following is a 32 bit data type?
int
What is the output by the code below? public class CS { public void one( ) { System.out.print("one"); } } //code in the main of another class CS test = new CS( ); test.one( );
one
What is the output by the code below? public class CS { public void one( ) { System.out.print("one"); } } //code in the main of another class CS test = new CS( ); test.one( ); test.one( );
oneone
What is the output by the code below? public class CS { public void one( ) { System.out.print("one"); } public void two( ) { System.out.print("two"); } } //code in the main of another class CS test = new CS( ); test.one( ); test.two( ); test.one( );
onetwoone