Computer Science Unit 7
What is returned by the call huh (11)?public static boolean huh(int n){for( int i = 2; i < n; i++ ){if(n % i == 0)return false; }return true;}
true
What is output by the code below?for(int i = 0; i < 3; i++){for(int j = -1; j < i+3; j++){System.out.print("*");}System.out.println();}
**** ***** ******
What is the output of the following code?for(int i = 0;i<5;i++){for(int j = 0;j<5;j++)if((j+i)%2==0)System.out.print("*");elseSystem.out.print(" ");System.out.println();}
***** ***** *****
What is output by the code below?int x = 0;do{System.out.print(x);x++;}while( x < 5 );
01234
Consider the following code segment.for(int i = 4; i < 8; i++){for(int j = 4-i; j >= 0; j--){System.out.print(j);}System.out.println(i);}What will be printed as a result of executing the code segment?
04 5 6 7
What is output by the code below?int x = 50;while(x > 10){ out.print(x+ " ");x = x / 4;}
50 12
What is it called when you put one loop inside another loop or an if inside of another if?
nesting
Consider the code that follows. Scanner scan = new Scanner("4 3 2 5 1");int sum = 0;while(scan.hasNextInt()){ /* blank */}Which of the following would correctly fill /* blank */ so that the code would sum all values in the Scanner?I.sum += scan.nextDouble();II.sum += scan.nextInt();III.System.out.println(scan.nextInt());
I and II only
What is the output of the following code?for(int i = 0;i<5;i++){for(int j = 4;j>i;j--)System.out.print(" ");for(int j = 5-i;j<=5;j++)System.out.print("*");System.out.println();}
* ** *** **** *****
What is returned by the call go( 2 ) ?public static String go(int x ){String s = "";for( int i = 1; i <= x; i++ ){ for( int j = 1; j <= i; j++ )s += "# "; s += "=\n";}return s;}
# = # # =
What is returned by the call go( 2 ) ?public static String go(int x ){String s = "";for( int i = 1; i <= x; i++ ){ for( int j = 1; j <= i; j++ )s += "# "; s += "=\n";}return s;}
#= ##=
What is returned by the call go( 4 ) ?public static String go(int x ){String s = "";for( int i = 1; i <= x; i++ ){ for( int j = 1; j <= i; j++ )s += "# "; s += "=\n";}return s;}
#= ##= ###= ####=
What is returned by the call go ( 5 ) ?public static String go(int x ){String s = "";for( int i = 1; i <= x; i++ ){ for( int j = 1; j <= i; j++ )s += "# "; s += "=\n";}return s;}
#= ##= ###= ####= #####=
What is the output of the following code?for(int i = 0;i<5;i++){for(int j = 0;j<5;j++)if(j==i)System.out.print("*");elseSystem.out.print(" ");System.out.println();}
* * * * *
What is the output of the following code?for(int i = 0;i<5;i++){for(int j = 0;j<5;j++)if((j+i)%2==0&&Math.abs(j-2)+1>j/2)System.out.print("*");elseSystem.out.print(" ");System.out.println();}
* * * * * * * * * *
What is the output of the following code?for(int i = 0;i<5;i++){for(int j = 0;j<5;j++)if((j+i)%2==0)System.out.print("*");elseSystem.out.print(" ");System.out.println();}
* * * * * * * * * * * * *
What is the output of the following code?for(int i = 0;i<5;i++){for(int j = 0;j<5;j++)if((j+i)%2==0)System.out.print(" ");elseSystem.out.print("*");System.out.println();}
** *** ** *** **
What is output by the code below?Scanner s = new Scanner("a 2 f q 7e 8 r 2 a");int count = 0;while(s.hasNext()){if (count % 3 == 1)count++;s.next();}System.out.println(count);
0
What is output by the code below? for(int ab=0; ab <= 2; ab++){ for(int cd=1; cd <= 3; cd++) System.out.print( ab % cd );}
000011002
What is output by the code below? for(int ab=0; ab <= 3; ab++){ for(int cd=1; cd <= 3; cd++) System.out.print( ab % cd );}
000011002010
What is output by the code below? for(int ab=0; ab <= 1; ab++){ for(int cd=1; cd <= 2; cd++) System.out.print( ab % cd );}
0001
How many stars are output by the code below?for(int i = 0;i<10;i++)for(int j = 0;j<10;j++)System.out.print("*");
100
What is output by the code below?int x = 2;int total = 0;do{total = total + x;x++;}while( x < 7);out.println(total);
20
What is output by the code below?int x = 1; do {x++;System.out.print(x);}while( x < 5);
2345
What is output by the code below?int x = 1;int total = 0;do{total = total + x; x = x + 5;}while(x <= 20);out.print( total );
34
What is the output by the code below using the following data file?nums.dat34231122Scanner file = new Scanner(new File("nums.dat"));System.out.println(file.nextInt()+" "+file.nextLine());
34
What is the output by the code below using the following data file?nums.dat34231122Scanner file = new Scanner(new File("nums.dat"));System.out.println(file.nextInt()+" "+file.next());
34 23
What is output by the code below?int x = 20,total = 0;while(x >= 11){total = total + x;x = x - 5;}out.print(total);
35
How many stars are output by the code below?for(int i = 0;i<10;i++)for(int j = 0;j<i;j++)System.out.print("*");
45
What is output by the code below?int total = 0;for(int i = 1; i <= 10; i = i + 3)for(int x = 1; x <= i; x = x + 2)total = total + x;out.println(total);
46
What is output by the code below?int x = 13;int total = 0;do{total = total + x; x = x - 2;}while(x > 0);out.println(total);
49
How many stars are output by the code below?for(int i = 0;i<10;i++)for(int j = i;j<10;j++)System.out.print("*");
55
What is output by the code below?int cnt = 0;for(int i=0; i<=10; i++ ){if(i%2 == 0) cnt++;}out.println(cnt);
6
What is methd huh() trying to determine about parameter n?public static boolean huh(int n){for( int i = 2; i < n; i++ ){if(n % i == 0)return false; }return true;}
It is trying to determine if n is an prime number.
What is the output by the code below using the following data file?nums.dat534231122Scanner file = new Scanner(new File("nums.dat"));int size = file.nextInt();for(int i=1; i <= size; i++){int num = file.nextInt();out.print(num + " ");}
There is no output due to a runtime error.
What is output by the code below?Scanner s = new Scanner("a 2 f q 7e 8 r 2 a");int count = 0;while(s.hasNext()){if (count % 3 == 1)out.print(s.next() + " ");count++;}
a 2 f q 7e 8 r 2 a
What is output by the code below?String st = "dog";for(int a=0; a<st.length(); a++){for(int b=1;b<=a;b++){System.out.print(" ");}for(int c=a;c<=st.length()/2+1;c++){System.out.print(st.charAt(a));}}
ddd oo g
What is output by the code below?String s = "dog";for(int x=0; x<s.length(); x++){for(int k=0;k<=x;k++){System.out.print(s.charAt(x));}}
dooggg
What is returned by the call huh (256) ?public static boolean huh(int n){for( int i = 2; i < n; i++ ){if(n % i == 0)return false; }return true;}
false
What is output by the following code?String s = "h";int x = 0;do{s = s + "h";x++;}while(!s.equals("hhhhh"));out.println(s + " - " + x);
hhhhh - 4