Deck 1: Review of Java Fundamentals

Full screen (f)
exit full mode
Question
Which of the following loop headers will arrange for the loop body to execute exactly 10 times?

A)for (int i = 1;i < 10;++i)
B)for (int i = 0;i <= 10;++i)
C)for (int i = -5;i < 5;++i)
D)for (int i = 2;i < 20;++i)
Use Space or
up arrow
down arrow
to flip the card.
Question
Which access modifier,used when defining a method,indicates that only one such method is available for all instances of the class?

A)final
B)private
C)protected
D)static
Question
When using the method System.out.printf( ),what is the purpose of the %d format code?

A)For printing a double
B)For printing a float
C)For printing a String
D)For printing an int
Question
Suppose c1 and c2 are objects of the class Circle.A Circle has a single data member,its radius.The Circle class has a default constructor (implemented correctly),but no other methods have been defined in the implementation of the Circle class.What will happen when we try to execute this code? Circle c1 = new Circle(12.0);
Circle c2 = new Circle(12.0);
Boolean same = (c1.equals(c2));

A)The code will not compile because equals( )has not been implemented in Circle.
B)The value of same will be true.
C)The value of same will be false.
Question
In Java,how do we tell the compiler that the body of a loop consists of several statements,rather than one?

A)We enter all the statements on the same line
B)We indent all the statements at the same level of indentation
C)We enclose the statements in curly braces
D)We insert a break statement at the end of the loop
Question
In order to declare a named constant,the declaration must use which Java keyword?

A)final
B)int
C)static
D)void
Question
Which type of loop is guaranteed to execute its body at least once?

A)do-while
B)for
C)switch
D)while
Question
A comment in Java that begins with /** and ends with */ is what kind of comment?

A)block comment
B)javadoc comment
C)line comment
D)nested comment
Question
Suppose a String variable s is initialized to the value "inheritance".What value is returned by the call s.substring(2,5)?

A)nher
B)nheri
C)her
D)heri
Question
If s1 is of type String,what does s1.compareTo(s1)return?

A)zero
B)true
C)false
D)Cannot be determined without knowing the value of s1.
Question
What feature of Java transforms class objects into a sequence of bytes that may be used by another program?

A)compilation
B)inheritance
C)serialization
D)tokenization
Question
What does it mean for the return type of a method to be void?

A)The method will never return a value.
B)The method will return the value zero.
C)The method does not take parameters.
D)The method does not have a body.
Question
What type of Java statement allows you to use classes contained in other packages?

A)an access statement
B)a class statement
C)an import statement
D)a package statement
Question
Suppose a try block needs to be followed by two catch blocks,each catching a different exception.Which exception should be caught first?

A)The exception that is more likely to occur
B)The exception that is more general
C)The exception that is more specific
D)It does not matter in what order exceptions are caught
Question
What is the meaning of the declaration: String [][] a = new String [60][80];?

A)Create an array of 60 strings,each of size 80 characters.
B)Create an array of 80 strings,each of size 60 characters.
C)Create a two-dimensional array of strings with 60 columns and 80 rows.
D)Create a two-dimensional array of strings with 60 rows and 80 columns.
Question
How many constructors can a class have?

A)Exactly one
B)At least one but no more than three
C)Exactly the same as the number of data members
D)There is no restriction on the number of constructors
Question
Which of the following is an example of a wrapper class?

A)Double
B)int
C)String
D)System
Question
Which of these expressions is illegal in Java?

A)x++ 5
B)x =+ 5
C)x += 5
D)x == 5
Question
Suppose s is of type String.What would it mean if s.lastIndexOf)returns the value 1?

A)The first character appears once in the string.
B)The first two characters in the string are identical.
C)The length of the string is 2.
D)The second character of the string is '0'.
Question
If x is a variable of type int,what is the largest possible value of the expression (x % 5)?

A)1
B)4
C)5
D)2³¹-1
Question
Short-circuit evaluation refers to:

A)Jumping from the try block to the catch block when an exception is thrown.
B)Avoiding the testing of a boolean condition that is unnecessary.
C)Truncating the integer result of a division operation.
D)Avoiding the execution of the else clause of an if-statement.
Question
The Java expression 9 / 5 + 9 % 5 equals ______.

A)0
B)1
C)3
D)5
E)6
Question
If we wanted to write an if-statement that executes whenever the real number x is between 10.0 and 20.0,how should the test condition be written?

A)10.0 < x || x > 20.0
B)10.0 < x && x > 20.0
C)10.0 < x && x < 20.0
D)10.0 < x || x < 20.0
Question
All Java classes must contain a method called main.
Question
Comments beginning with the characters // can extend for multiple lines until the compiler encounters \\.
Question
The Java expression (75 - 63)* 10 / 6 - 1 evaluates to 19.
Question
In Java,when we write an if-statement of the form if(condition),the condition must evaluate to a boolean value.
Question
If d is a double and i is an int,then the assignment statement d = i;is legal in Java.
Question
Which of these is not a legal Java identifier?

A)2be
B)to_be
C)TOBE
D)tobE
Question
A default constructor requires at least one parameter in order to compile correctly.
Question
All Java programs must define at least one class.
Question
A built-in class that helps to split strings into pieces,such as words of a sentence,is:

A)Console
B)Scanner
C)StringBuffer
D)StringTokenizer
Question
Consider the following code that appears in a test class. A a = new A();
Int c = a.b;
In order for this code to work,which statement must be true?

A)a must be declared public inside class A
B)b must be declared public inside class A
C)c must be declared public inside class A
D)Method b( )must return int
Question
Which is not a primitive type in Java?

A)String
B)float
C)double
D)long
Question
Integer literals beginning with the digit 0 are interpreted to be in decimal notation.
Question
If an int is added to a float,the result will be an int.
Question
A statement invoking a constructor should also use the Java keyword ______.

A)class
B)return
C)public
D)new
Question
How is the finally keyword used in Java?

A)To indicate that a method should terminate and pass a value to the calling environment.
B)To indicate the last statement that will execute in a program.
C)To indicate an action that should take place whether an exception occurred or not.
D)To indicate a termination condition for a loop.
Question
All classes extend which built-in class?

A)Main
B)Object
C)Simple
D)Super
Question
If s1 = "dog" and s2 = "cat",then s1.compareTo(s2)returns a positive integer value.
Question
If s = "hello,world" (with exactly one space between the comma and 'w'),what does s.indexOf(",")return?
Question
Suppose a class Planet had a method findLife( )that we call as follows in main( ):
int value = p.findLife("goat",true,0.5);
How would the findLife( )method be declared in Planet.java?
Question
Write a for-loop that will print all the positive integers from 100 down to 1,inclusive,one number per line.
Question
Suppose a,b and c are the lengths of the 3 sides of a triangle.Write an if-statement that will determine if the triangle is isosceles (at least 2 of the 3 sides are equal).You may assume that a,b and c are of type int.
Question
What is wrong with this Java statement? int num = new int(5);
Question
Why does the Java statement System.out.println("answer = " + 3 + 4);
not print answer = 7 ?
Question
Interpret the overall meaning of this if-statement:
if (num % 7 == 0 || num % 11 == 0)
Question
Suppose c is a variable of type char.We want to know if c is a lowercase vowel letter (a/e/i/o/u).What is wrong with the following comparison?
if (c == 'a' || 'e' || 'i' || 'o' || 'u')
Question
How many times are the indicated statements (#1)and #2)each executed?
for (int i = 1;i <= 10;++i)
for (int j = 1;j <= 10;++j)
for (int k = 1;k <= 5;++k)
++count; // statement #1
System.out.printf("%d\n",count);// statement #2
Question
The following code containing a loop attempts to find how many times the letter 'r' appears in a string.But something is wrong with the loop.How would you fix it?
String s = "railroad";
int count = 0;
char letter = s.charAt(index);
for (int index = 0;index < s.length();++index)
if (letter == 'r')
++count;
Question
What is the value of sum after the following code executes?
int sum = 0;
int count = 0;
while (count < 4)
{
sum += count / 2;
count += 1;
}
Question
Complete the following code so that it sets found to true if the array a consisting of integers contains the value zero.
int index = 0;
boolean found = false;
Question
Suppose s1 is a String variable.We want to check to see if the first and last characters of s1 are the same.Complete the following if-statement to accomplish the task.
boolean same;
if (______________________________________________)
same = true;
else
same = false;
Question
Suppose temp is an array of 12 double values that holds the average temperatures of the 12 months of the year,January through December,in that order.Use Java to find the average of the temperatures for the 3 summer months,June,July and August only,and set this answer to the variable summerAverage.
Question
The following code attempts to find the sum of the elements in the third column (from the left)of a two dimensional int array called a that has 10 rows and 20 columns.Correct the errors in the code.
int sum = 0;
for (int i = 0;i < 20;i++)
sum = sum + a[3][i];
Question
Suppose a is a one-dimensional array of double.Show how you would find the largest element of a using Java code.
Question
What will happen when you try to run a program that has a syntax error?
Question
What is wrong with this loop? How would you fix it?
int num = 1;
while (num <= 10){
System.out.println("num = " + num);
}
Question
Suppose x and y are int variables.Write a statement that declares the boolean variable between,and sets this variable equal to true if the value of y is between 0 and x,inclusive,and equal to false otherwise.(Assume that you don't know if x is positive or negative. )
Question
A leap year occurs when the year number is divisible by 4.But there is a special case for years ending in 00: these must be divisible by 400 to be considered a leap year.Thus,1900 was not a leap year,but 2000 was.Write an if-statement that determines if the integer variable year represents a leap year.(Hint: use the % operator for taking remainders. )
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/60
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 1: Review of Java Fundamentals
1
Which of the following loop headers will arrange for the loop body to execute exactly 10 times?

A)for (int i = 1;i < 10;++i)
B)for (int i = 0;i <= 10;++i)
C)for (int i = -5;i < 5;++i)
D)for (int i = 2;i < 20;++i)
C
2
Which access modifier,used when defining a method,indicates that only one such method is available for all instances of the class?

A)final
B)private
C)protected
D)static
D
3
When using the method System.out.printf( ),what is the purpose of the %d format code?

A)For printing a double
B)For printing a float
C)For printing a String
D)For printing an int
D
4
Suppose c1 and c2 are objects of the class Circle.A Circle has a single data member,its radius.The Circle class has a default constructor (implemented correctly),but no other methods have been defined in the implementation of the Circle class.What will happen when we try to execute this code? Circle c1 = new Circle(12.0);
Circle c2 = new Circle(12.0);
Boolean same = (c1.equals(c2));

A)The code will not compile because equals( )has not been implemented in Circle.
B)The value of same will be true.
C)The value of same will be false.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
5
In Java,how do we tell the compiler that the body of a loop consists of several statements,rather than one?

A)We enter all the statements on the same line
B)We indent all the statements at the same level of indentation
C)We enclose the statements in curly braces
D)We insert a break statement at the end of the loop
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
6
In order to declare a named constant,the declaration must use which Java keyword?

A)final
B)int
C)static
D)void
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
7
Which type of loop is guaranteed to execute its body at least once?

A)do-while
B)for
C)switch
D)while
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
8
A comment in Java that begins with /** and ends with */ is what kind of comment?

A)block comment
B)javadoc comment
C)line comment
D)nested comment
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
9
Suppose a String variable s is initialized to the value "inheritance".What value is returned by the call s.substring(2,5)?

A)nher
B)nheri
C)her
D)heri
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
10
If s1 is of type String,what does s1.compareTo(s1)return?

A)zero
B)true
C)false
D)Cannot be determined without knowing the value of s1.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
11
What feature of Java transforms class objects into a sequence of bytes that may be used by another program?

A)compilation
B)inheritance
C)serialization
D)tokenization
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
12
What does it mean for the return type of a method to be void?

A)The method will never return a value.
B)The method will return the value zero.
C)The method does not take parameters.
D)The method does not have a body.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
13
What type of Java statement allows you to use classes contained in other packages?

A)an access statement
B)a class statement
C)an import statement
D)a package statement
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
14
Suppose a try block needs to be followed by two catch blocks,each catching a different exception.Which exception should be caught first?

A)The exception that is more likely to occur
B)The exception that is more general
C)The exception that is more specific
D)It does not matter in what order exceptions are caught
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
15
What is the meaning of the declaration: String [][] a = new String [60][80];?

A)Create an array of 60 strings,each of size 80 characters.
B)Create an array of 80 strings,each of size 60 characters.
C)Create a two-dimensional array of strings with 60 columns and 80 rows.
D)Create a two-dimensional array of strings with 60 rows and 80 columns.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
16
How many constructors can a class have?

A)Exactly one
B)At least one but no more than three
C)Exactly the same as the number of data members
D)There is no restriction on the number of constructors
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
17
Which of the following is an example of a wrapper class?

A)Double
B)int
C)String
D)System
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
18
Which of these expressions is illegal in Java?

A)x++ 5
B)x =+ 5
C)x += 5
D)x == 5
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
19
Suppose s is of type String.What would it mean if s.lastIndexOf)returns the value 1?

A)The first character appears once in the string.
B)The first two characters in the string are identical.
C)The length of the string is 2.
D)The second character of the string is '0'.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
20
If x is a variable of type int,what is the largest possible value of the expression (x % 5)?

A)1
B)4
C)5
D)2³¹-1
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
21
Short-circuit evaluation refers to:

A)Jumping from the try block to the catch block when an exception is thrown.
B)Avoiding the testing of a boolean condition that is unnecessary.
C)Truncating the integer result of a division operation.
D)Avoiding the execution of the else clause of an if-statement.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
22
The Java expression 9 / 5 + 9 % 5 equals ______.

A)0
B)1
C)3
D)5
E)6
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
23
If we wanted to write an if-statement that executes whenever the real number x is between 10.0 and 20.0,how should the test condition be written?

A)10.0 < x || x > 20.0
B)10.0 < x && x > 20.0
C)10.0 < x && x < 20.0
D)10.0 < x || x < 20.0
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
24
All Java classes must contain a method called main.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
25
Comments beginning with the characters // can extend for multiple lines until the compiler encounters \\.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
26
The Java expression (75 - 63)* 10 / 6 - 1 evaluates to 19.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
27
In Java,when we write an if-statement of the form if(condition),the condition must evaluate to a boolean value.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
28
If d is a double and i is an int,then the assignment statement d = i;is legal in Java.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
29
Which of these is not a legal Java identifier?

A)2be
B)to_be
C)TOBE
D)tobE
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
30
A default constructor requires at least one parameter in order to compile correctly.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
31
All Java programs must define at least one class.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
32
A built-in class that helps to split strings into pieces,such as words of a sentence,is:

A)Console
B)Scanner
C)StringBuffer
D)StringTokenizer
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
33
Consider the following code that appears in a test class. A a = new A();
Int c = a.b;
In order for this code to work,which statement must be true?

A)a must be declared public inside class A
B)b must be declared public inside class A
C)c must be declared public inside class A
D)Method b( )must return int
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
34
Which is not a primitive type in Java?

A)String
B)float
C)double
D)long
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
35
Integer literals beginning with the digit 0 are interpreted to be in decimal notation.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
36
If an int is added to a float,the result will be an int.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
37
A statement invoking a constructor should also use the Java keyword ______.

A)class
B)return
C)public
D)new
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
38
How is the finally keyword used in Java?

A)To indicate that a method should terminate and pass a value to the calling environment.
B)To indicate the last statement that will execute in a program.
C)To indicate an action that should take place whether an exception occurred or not.
D)To indicate a termination condition for a loop.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
39
All classes extend which built-in class?

A)Main
B)Object
C)Simple
D)Super
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
40
If s1 = "dog" and s2 = "cat",then s1.compareTo(s2)returns a positive integer value.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
41
If s = "hello,world" (with exactly one space between the comma and 'w'),what does s.indexOf(",")return?
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
42
Suppose a class Planet had a method findLife( )that we call as follows in main( ):
int value = p.findLife("goat",true,0.5);
How would the findLife( )method be declared in Planet.java?
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
43
Write a for-loop that will print all the positive integers from 100 down to 1,inclusive,one number per line.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
44
Suppose a,b and c are the lengths of the 3 sides of a triangle.Write an if-statement that will determine if the triangle is isosceles (at least 2 of the 3 sides are equal).You may assume that a,b and c are of type int.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
45
What is wrong with this Java statement? int num = new int(5);
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
46
Why does the Java statement System.out.println("answer = " + 3 + 4);
not print answer = 7 ?
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
47
Interpret the overall meaning of this if-statement:
if (num % 7 == 0 || num % 11 == 0)
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
48
Suppose c is a variable of type char.We want to know if c is a lowercase vowel letter (a/e/i/o/u).What is wrong with the following comparison?
if (c == 'a' || 'e' || 'i' || 'o' || 'u')
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
49
How many times are the indicated statements (#1)and #2)each executed?
for (int i = 1;i <= 10;++i)
for (int j = 1;j <= 10;++j)
for (int k = 1;k <= 5;++k)
++count; // statement #1
System.out.printf("%d\n",count);// statement #2
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
50
The following code containing a loop attempts to find how many times the letter 'r' appears in a string.But something is wrong with the loop.How would you fix it?
String s = "railroad";
int count = 0;
char letter = s.charAt(index);
for (int index = 0;index < s.length();++index)
if (letter == 'r')
++count;
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
51
What is the value of sum after the following code executes?
int sum = 0;
int count = 0;
while (count < 4)
{
sum += count / 2;
count += 1;
}
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
52
Complete the following code so that it sets found to true if the array a consisting of integers contains the value zero.
int index = 0;
boolean found = false;
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
53
Suppose s1 is a String variable.We want to check to see if the first and last characters of s1 are the same.Complete the following if-statement to accomplish the task.
boolean same;
if (______________________________________________)
same = true;
else
same = false;
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
54
Suppose temp is an array of 12 double values that holds the average temperatures of the 12 months of the year,January through December,in that order.Use Java to find the average of the temperatures for the 3 summer months,June,July and August only,and set this answer to the variable summerAverage.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
55
The following code attempts to find the sum of the elements in the third column (from the left)of a two dimensional int array called a that has 10 rows and 20 columns.Correct the errors in the code.
int sum = 0;
for (int i = 0;i < 20;i++)
sum = sum + a[3][i];
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
56
Suppose a is a one-dimensional array of double.Show how you would find the largest element of a using Java code.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
57
What will happen when you try to run a program that has a syntax error?
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
58
What is wrong with this loop? How would you fix it?
int num = 1;
while (num <= 10){
System.out.println("num = " + num);
}
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
59
Suppose x and y are int variables.Write a statement that declares the boolean variable between,and sets this variable equal to true if the value of y is between 0 and x,inclusive,and equal to false otherwise.(Assume that you don't know if x is positive or negative. )
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
60
A leap year occurs when the year number is divisible by 4.But there is a special case for years ending in 00: these must be divisible by 400 to be considered a leap year.Thus,1900 was not a leap year,but 2000 was.Write an if-statement that determines if the integer variable year represents a leap year.(Hint: use the % operator for taking remainders. )
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 60 flashcards in this deck.