Itsd322 data structures and


1. Which of the following statements will print a single line containing "hello there"?
a. System.out.println ( "hello" ); System.out.println( " there" );
b. System.out.println ( "hello" , " there" );
c. System.out.println ( "hello" ); System.out.print ( " there" );
d. System.out.print ( "hello" ); System.out.println( " there" );

2. What is the value of result after the following Java statements execute?
int a, b, c, d; a = 4;
b = 12;
c = 37;
d = 51;
result = d % a * c + a % b + a;
a. 119
b. 51
c. 127
d. 59

3. What will be output after the following Java statements have been executed?
int a, b, c, d; a = 4;
b = 12;
c = 37;
d = 51;
if ( a < b )
System.out.println( "a < b" ); if ( a > b )
System.out.println( "a > b" ); if ( d <= c )
System.out.println( "d <= c" ); if ( c != d )
System.out.println( "c != d" );

a. a < b c != d
b. a < b d <= c c != d
c. a > b c != d
d. a < b c < d a != b

4. What is output by the following Java code segment?
int temp; temp = 200;
if ( temp > 90 )
System.out.println( "This porridge is too hot." ); if ( temp < 70 )
System.out.println( "This porridge is too cold." ); if ( temp == 80 )
System.out.println( "This porridge is just right!" );
a. This porridge is too hot.

b. This porridge is too cold.
c. This porridge is just right!
d. None of the above.

5. What is output by the following Java code segment?
int temp; temp = 180;
if ( temp > 90 ) {
System.out.println( "This porridge is too hot." );
// cool down
temp = temp - ( temp > 150 ? 100 : 20 );
}
else {
if ( temp < 70 ) {
System.out.println("This porridge is too cold.");
// warm up
temp = temp + (temp < 50 ? 30 : 20);
}
}
if ( temp == 80 )
System.out.println( "This porridge is just right!" );
a. This porridge is too hot.
b. This porridge is too cold. This porridge is just right!
c. This porridge is just right!
d. None of the above.

6. What is output by the following Java code segment?
int temp; temp = 180;
while ( temp != 80 ) { if ( temp > 90 ) {
System.out.print( "This porridge is too hot! " );
// cool down
temp = temp - ( temp > 150 ? 100 : 20 );
}
else {
if ( temp < 70 ) { System.out.print(
"This porridge is too cold! ");
// warm up
temp = temp + (temp < 50 ? 30 : 20);
}
}
}
if ( temp == 80 )
System.out.println( "This porridge is just right!" );
a. This porridge is too cold! This porridge is just right!
b. This porridge is too hot! This porridge is just right!
c. This porridge is just right!
d. None of the above.

7. A Java class can have which of the following methods?
A. foo( int a )
B. foo( int a, int b )
C. foo( double a )
D. foo( double a, double b )

E. foo( int b )
a. All of the above.
b. A, B, D, E.
c. A, B, C, D.
d. A, C, D, E.
8. Which of the following statements about arrays are true?
A. Arrays are a group of contiguous memory locations, where each location is an element.
B. Elements are located by subscript or index.
C. The length of array c is determined by the expression c.length();
D. The seventh element of array c is specified by c[7].
a. A, C, D.
b. A, B.
c. C, D.
d. A, B, C, D.

9. Consider the array:

s[0]

7

s[1]

0

s[2]

-12

s[3]

9

s[4]

10

s[5]

3

s[6]

6

The value of s[ s[6] - s[5] ] is:
a. 0.
b. 3.
c. 9.
d. 0.

10. Consider the code segment below. Which of the following statements is not true.
1. int g[];
2. g = new int[ 23 ];
a. Statement 1 declares an array reference.
b. Statement 2 allocates the array.
c. g is a reference to an array of integers.
d. The value of g[ 3 ] is -1.

11. Which of the following initializer lists would correctly set the elements of array n? a. int n[] = {1, 2, 3, 4, 5 };
b. array n[ int ] = { 1, 2, 3, 4, 5 };
c. int n[ 5 ] = {1; 2; 3; 4; 5 };
d. int n = new int( 1, 2, 3, 4, 5 );

12. Consider the class below.
public class Test {
public static void main( String args[] )
{
int a[];
a = new int[ 10 ];
for ( int i = 0; i < a.length; i++ ) a[ i ] = i + 1 * 2;
int result = 0;
for ( int i = 0; i < a.length; i++ ) { result += a[ i ];

}
System.out.println( "Result is : " + result ); System.exit( 0 );
}
}
The output of this Java program will be:
a. Result is : 62.
b. Result is : 64.
c. Result is : 65.
d. Result is : 67.
13. Consider the program below:
public class Test {
public static void main( String args[] )
{
int a[] = { 99, 22, 11, 3, 11, 55, 44, 88, 2, -3 };
int result = 0;
for ( int i = 0; i < a.length; i++ ) { if ( a[ i ] > 30 )
result += a[ i ];
}
System.out.println("Result is : " + result ); System.exit( 0 );
}
}
The output of this Java program will be:
a. Result is : 280.
b. Result is : 154.
c. Result is : 286.
d. Result is : 198.
14. Constructors:
a. Have the same name as the class.
b. May not specify a return type.
c. Are called automatically when an object is instantiated.
d. All of the above.

15. An instance variable has which of the following scopes?
a. Block scope.
b. Method scope.
c. Class scope.
d. None of the above.

16. A package is:
a. A directory structure used to organize classes and interfaces.
b. A mechanism for software reuse.
c. A group of related classes and interfaces.
d. All of the above.

17. A constructor cannot:
a. Be overloaded.
b. Initialize variables to their defaults.
c. Specify return types or return values.
d. Have the same name as the class.

18. Instance variables declared final do not or cannot:
a. Use the principle of least privilege.

b. Be initialized.
c. Be modified.
d. Cause syntax errors if used as a left-hand value.

19. Develop a complete Java program that uses if-else constructs and a class called "Display" to prompt the user for an integer number. If the number is a negative integer, the program will call on the displayInfo() method from the class "Display" to display your first name, your last name, and the number, and then terminates. If the number is positive integer, the program will call on the displayInfo() method from the class "Display" to display your last name, your first name, and then terminates, otherwise, the program displays the string "Nothing processed...", and then terminates. You must use Exception Handling to detect and entry other than numeric value (InputMismatchException) and a General Exception.

Solution Preview :

Prepared by a verified Expert
Data Structure & Algorithms: Itsd322 data structures and
Reference No:- TGS02384377

Now Priced at $20 (50% Discount)

Recommended (95%)

Rated (4.7/5)