Ifs Test
By Mickey Arnold
starstarstarstarstarstarstarstarstarstar
Last updated 10 months ago
10 Questions
15 points
15
Question 1
1.
Given a word with no spaces, determine the location of the ? in the word and then create and return a new String that does not contain a ?.Part A – Write method findQuestionMark that will return the index position of the ? in the parameter word. All words with have at most one ?
The call findQuestionMark("dog?cat") would return 3.The call findQuestionMark("?bigbird") would return 0.The call findQuestionMark("squarepumpkin?") would return 13.The call findQuestionMark("porkypig") would return -1.The call findQuestionMark("f?atcatsonthemat") would return 1.
public static int findQuestionMark( String word ){ //Code goes here//}
Given a word with no spaces, determine the location of the ? in the word and then create and return a new String that does not contain a ?.
Part A – Write method findQuestionMark that will return the index position of the ? in the parameter word. All words with have at most one ?
The call findQuestionMark("dog?cat") would return 3.
The call findQuestionMark("?bigbird") would return 0.
The call findQuestionMark("squarepumpkin?") would return 13.
The call findQuestionMark("porkypig") would return -1.
The call findQuestionMark("f?atcatsonthemat") would return 1.
public static int findQuestionMark( String word )
{
//Code goes here//
}
15 points
15
Question 2
2.
Part B – Write method removeQuestionMark that will return a new String based on the parameter word that does not contain a ?. You must call method findQuestionMark from part A. Assume that part A works as intended regardless of what you wrote in part A.
The call removeQuestionMark( "fred?flintstone" ) would return fredflintstone.The call removeQuestionMark( "howardthe?goose" ) would return howardthegoose.The call removeQuestionMark( "?woweeistheword" ) would return woweeistheword.The call removeQuestionMark( "fatcatsonthemat" ) would return fatcatsonthemat.
public static String removeQuestionMark( String word ){ //Code goes here//}
Part B – Write method removeQuestionMark that will return a new String based on the parameter word that does not contain a ?. You must call method findQuestionMark from part A. Assume that part A works as intended regardless of what you wrote in part A.
The call removeQuestionMark( "fred?flintstone" ) would return fredflintstone.
The call removeQuestionMark( "howardthe?goose" ) would return howardthegoose.
The call removeQuestionMark( "?woweeistheword" ) would return woweeistheword.
The call removeQuestionMark( "fatcatsonthemat" ) would return fatcatsonthemat.
public static String removeQuestionMark( String word )
{
//Code goes here//
}
8.75 points
8.75
Question 3
3.
What is returned by the call go(60,78)?public static String go(int x, int z){ String s = "xyz"; if(z > 60) { s += "def"; } if(x <= 60) s += "ghi"; return s;}
What is returned by the call go(60,78)?
public static String go(int x, int z)
{
String s = "xyz";
if(z > 60)
{
s += "def";
}
if(x <= 60)
s += "ghi";
return s;
}
8.75 points
8.75
Question 4
4.
Consider the following code segment.
String s = "red ball";if(s.indexOf("a")>0) s = s + " zed";if(s.indexOf("z")>0) s = "alpha " + s;out.println(s);
What will be printed as a result of executing the code segment?
Consider the following code segment.
String s = "red ball";
if(s.indexOf("a")>0)
s = s + " zed";
if(s.indexOf("z")>0)
s = "alpha " + s;
out.println(s);
What will be printed as a result of executing the code segment?
8.75 points
8.75
Question 5
5.
Consider the following code segment.
String s = "abac-adae-bfbg-bhbi";int index = s.indexOf("b");if(index > 0) out.println(s.substring(index));
What will be printed as a result of executing the code segment?
Consider the following code segment.
String s = "abac-adae-bfbg-bhbi";
int index = s.indexOf("b");
if(index > 0)
out.println(s.substring(index));
What will be printed as a result of executing the code segment?
8.75 points
8.75
Question 6
6.
What is returned by the call go(7) ?public static int go( int a ){ int ans = 0; if(a>=7) { ans += 1; } ans += 2; return ans;}
What is returned by the call go(7) ?
public static int go( int a )
{
int ans = 0;
if(a>=7)
{
ans += 1;
}
ans += 2;
return ans;
}
8.75 points
8.75
Question 7
7.
What is output by the code below?String a = "1";String b = new String("1");if( a == b ){ System.out.print( "up" );}if( a.equals(b) ){ System.out.print( "on" );}System.out.print( "at" );
What is output by the code below?
String a = "1";
String b = new String("1");
if( a == b )
{
System.out.print( "up" );
}
if( a.equals(b) )
{
System.out.print( "on" );
}
System.out.print( "at" );
8.75 points
8.75
Question 8
8.
Consider the following code segment.double x = 19.8;int y = 0;if( x > 5 ) y += 5;if( x > 10 ) y += 10;if( x > 15 ) y += 15;out.print( y );What will be printed as a result of executing the code segment?
Consider the following code segment.
double x = 19.8;
int y = 0;
if( x > 5 )
y += 5;
if( x > 10 )
y += 10;
if( x > 15 )
y += 15;
out.print( y );
What will be printed as a result of executing the code segment?
8.75 points
8.75
Question 9
9.
Consider the following code segment.String s = "even";if(s.indexOf("a")>0) s = s + " zed";if(s.indexOf("v")>0) s = "alpha " + s;out.println(s);What will be printed as a result of executing the code segment?
Consider the following code segment.
String s = "even";
if(s.indexOf("a")>0)
s = s + " zed";
if(s.indexOf("v")>0)
s = "alpha " + s;
out.println(s);
What will be printed as a result of executing the code segment?
8.75 points
8.75
Question 10
10.
Consider the following code segment.String s = "apple";if(s.indexOf("a")>0) s = s + " zed";if(s.indexOf("l")>0) s = "alpha " + s;out.println(s);
Consider the following code segment.
String s = "apple";
if(s.indexOf("a")>0)
s = s + " zed";
if(s.indexOf("l")>0)
s = "alpha " + s;
out.println(s);