Levels Tought:
Elementary,Middle School,High School,College,University,PHD
Teaching Since: | Apr 2017 |
Last Sign in: | 3 Weeks Ago, 6 Days Ago |
Questions Answered: | 7559 |
Tutorials Posted: | 7341 |
BS,MBA, PHD
Adelphi University/Devry
Apr-2000 - Mar-2005
HOD ,Professor
Adelphi University
Sep-2007 - Apr-2017
1. Assuming a StringIndexOutOfBoundsException exception class exists, what is the output of this code sequence?
try
{
String word = new String("Java");
System.out.println( word.charAt( 4 ) );
}
catch( StringIndexOutOfBoundsException e )
{
System.out.println( “OOPS!” );
}
finally
{
System.out.println( “OOPS! again” );
}
|
a. |
OOPS! |
|
b. |
a OOPS! again |
|
c. |
a |
|
d. |
OOPS! OOPS! again |
2.5 points
QUESTION 2
1. In the method header public static void main( String [ ] args )
|
a. |
variable identifier args is an array of type String |
|
b. |
variable identifier args could contain arguments passed to method main from the command line |
|
c. |
Both a and b |
|
d. |
None of the above |
2.5 points
QUESTION 3
1. In a typical class, what is the general recommendation for access modifiers?
|
a. |
Instance variables are are private and methods are private |
|
b. |
Instance variables are private and methods are public |
|
c. |
Instance variables are public and methods are private |
|
d. |
Instance variables are public and methods are public |
2.5 points
QUESTION 4
1. What is the output of the following code sequence:
double a = 27 % 11;
System.out.println( a );
|
a. |
297.0 |
|
b. |
2.0 |
|
c. |
5.0 |
|
d. |
2.45 |
2.5 points
QUESTION 5
1. In the Quiz class, the foo method has the following API:
public static double foo( int i)
Assuming x is an integer value, and assuming the following statement having been executed:
Quiz q = new Quiz();
which method call(s) could be used?
|
a. |
double i = Quiz.foo( x ); |
|
b. |
System.out.println(“ Foo has returned the double value: “ + q.foo( x)); |
|
c. |
both a and b |
|
d. |
neither a or b |
2.5 points
QUESTION 6
1. To instantiate an object reference named d1 of class SimpleDate assuming the following constructor method headers:
public SimpleDate()
public SimpleDate( int mm, int dd, int yyyy )
Which statement(s) would be valid and sufficient to instantiate an object of type SimpleDate:
|
a. |
SimpleDate d1; |
|
b. |
SimpleDate d1 = new SimpleDate(); |
|
c. |
SimpleDate d1; d1 = new SimpleDate( 7, 4, 1776); |
|
d. |
b and c |
2.5 points
QUESTION 7
1. What is the output of the following code sequence?
int dice = 5;
switch ( dice )
{
case 1:
System.out.println( “One”);
break;
case 2:
case 3:
System.out.println( “Two or Three”);
break;
case 4:
System.out.println( “Four”);
break;
case 5:
case 6:
System.out.println( “Five or better”);
break;
default:
System.out.println( “Off the chart”);
}
System.out.println( “Exit Switch” );
|
a. |
Five or better |
|
b. |
Exit Switch |
|
c. |
Five or better |
|
d. |
Five or better |
2.5 points
QUESTION 8
1. What are the values of i and product after this code sequence is executed?
int i = 6;
int product = 1;
do
{
product *= i;
i++;
} while (i < = 9 );
|
a. |
i is 9 |
|
b. |
i is 9 |
|
c. |
i is 10 |
|
d. |
i is 10 |
2.5 points
QUESTION 9
1. Given the following code declaring and initializing three boolean variables x, y and z with respective values false, false, and false, answer question 9 indicating whether the value of the expression is true or false.
boolean x = false;
boolean y = false;
boolean z = false;
Expression: x || !z
True
False
2.5 points
QUESTION 10
1. What is the output of this code sequence?
boolean x = true;
boolean y = false;
for ( int i = 0; i < 2; i++)
System.out.println( “Hello” );
if( x && y)
System.out.println( “Done” );
else
System.out.println( “Shall we go sailing?” );
|
a. |
Hello |
|
b. |
Hello |
|
c. |
Hello |
|
d. |
Hello |
2.5 points
QUESTION 11
1. What are the values of i and sum after this code sequence is executed?
int i = 0;
int sum = 0;
for ( i = 0; i < = 40; i++)
{
if ( i % 10 = = 0)
sum += i ;
}
|
a. |
i is 40 |
|
b. |
i is 41 |
|
c. |
i is 40 |
|
d. |
i is 41 |
2.5 points
QUESTION 12
1. What is the output of the following code sequence:
double a = 16 / 5;
System.out.println( a );
|
a. |
2.6 |
|
b. |
3.2 |
|
c. |
a |
|
d. |
3.0 |
2.5 points
QUESTION 13
1. Given code:
public abstract class A{ }
public class B extends A
{
}
The following code sequence would correctly create an object reference of class A holding an object reference for an object of class B:
A c;
c = new B();
True
False
2.5 points
QUESTION 14
1. Given the following code declaring and initializing two int variables x and y with respective values 7 and 9, answer question 14 indicating whether the value of the expression is true or false.
int x = 7;
int y = 9;
Expression: ( y – x == 0 || ( x + 2 ) != y)
True
False
2.5 points
QUESTION 15
1. Class SimpleDate encapsulates integer instance variables month, day and year. Consider the following equals method in class SimpleDate:
public boolean equals( Object o )
{
if ( !( d instanceof SimpleDate ) )
return false;
( Insert appropriate code from selections below)
if ( month == d1.month
&& day == d1.day
&& year == d1.year )
return true;
else
return false;
}
Which statement(s) could be used to complete the above method body code correctly?
|
a. |
SimpleDate d1; |
|
b. |
SimpleDate d1 = o; |
|
c. |
SimpleDate d1 = (SimpleDate)o; |
|
d. |
Both a and c |
2.5 points
QUESTION 16
1. Which of the following is not a valid wrapper class?
|
a. |
Integer |
|
b. |
Double |
|
c. |
Char |
|
d. |
Boolean |
2.5 points
QUESTION 17
1. What is the output of this code sequence:
double [ ][ ] a =
{{ 12.5, 48.3, 65.0, 77.1 },
{ 65.0, 48.3, 12.5}};
System.out.println( a[1][ 3 ] );
|
a. |
An ArrayIndexOutOfBounds exception would be thrown. |
|
b. |
77.1 |
|
c. |
65.0 |
|
d. |
12.5 |
2.5 points
QUESTION 18
1. What is the output of this code sequence?
double [ ] [ ] a =
{{ 100.4, 99.6, 48.2, 65.8 },
{ 100.5, 99.7, 48.2, 65.8 }};
double temp = a[0][1];
for ( int i = 1; i < a.length; i++ )
for ( int j = 1; j < a[i].length; j++ )
{
if ( a[i][j] > temp )
temp = a[i][j];
}
System.out.println( temp );
|
a. |
99.6 |
|
b. |
100.4 |
|
c. |
99.7 |
|
d. |
100.5 |
2.5 points
QUESTION 19
1. Which statement(s) will instantiate a double array of 6 total elements?
|
a. |
double [] [] cellbills = new double[2][]; |
|
b. |
double [] [] cellbills = { { 45.24, 54.67, 42.55, 44.61, 65.29 }, { 7 } }; |
|
c. |
Both a and b |
|
d. |
None of the above |
2.5 points
QUESTION 20
1. Given an array int a [][] = new int[3][4] ,how do you access the element of array a located in the third row and the fourth column?
|
a. |
a[3][4] |
|
b. |
a( 2,3 ) |
|
c. |
a{3}{4} |
|
d. |
a[2][3] |
2.5 points
QUESTION 21
1. Which statement will return the number of rows in a two dimensional array a?
|
a. |
int a = a.rows |
|
b. |
int a = a.length |
|
c. |
int a = a[i].length |
|
d. |
int a = a.size |
2.5 points
QUESTION 22
1. What statement(s) could you use to declare and instantiate an ArrayList object reference of Book objects named library with a default capacity of 10 elements?
|
a. |
ArrayList<Book > library = new ArrayList<Book>(); |
|
b. |
ArrayList<Book > library; library = new ArrayList<Book>(); |
|
c. |
both a and b |
|
d. |
neither a or b |
2.5 points
QUESTION 23
1. What method of an arraylist class object returns the number of elements in an ArrayList object?
|
a. |
size() |
|
b. |
capacity |
|
c. |
length |
|
d. |
length( ) |
2.5 points
QUESTION 24
1. After the following code sequence is executed, what are the contents at ArrayList index 1 in a?
ArrayList<Integer> a = new ArrayList<Integer>( );
a.add( 7 );
a.add( 10 );
a.add( 21 );
a.set( 1, 4 );
a.remove( 0 );
|
a. |
10 |
|
b. |
7 |
|
c. |
4 |
|
d. |
21 |
2.5 points
QUESTION 25
1. Which of the following is inherited by a subclass
|
a. |
all instance variables and methods |
|
b. |
public instance variables and methods only |
|
c. |
protected instance variables and methods only |
|
d. |
protected and public instance variables and methods |
2.5 points
QUESTION 26
1. Public Constructor methods
|
a. |
Are inherited by but cannot be accessed by a subclass |
|
b. |
Are inherited by and can be accessed with the super keyword by a subclass |
|
c. |
Are accessible with the super keyword by a subclass but are not inherited |
|
d. |
Are not inherited by and cannot be accessed by a subclass |
2.5 points
QUESTION 27
1. Assuming the class B extends class A, then
|
a. |
An object of class B is an object of class A |
|
b. |
An object of class A is an object of class B |
|
c. |
Both a and b |
|
d. |
None of the above |
2.5 points
QUESTION 28
1. What is the output of this code sequence?
Scanner parse = new Scanner("AA%:BB:CC");
parse.useDelimiter(":");
while(parse.hasNext())
{
System.out.print(parse.next() + " ");
System.out.println();
}
|
a. |
AA% |
|
b. |
AA% BB CC |
|
c. |
AA% |
|
d. |
None of the above |
2.5 points
QUESTION 29
1. It is legal to have more than one catch block to match a try block but you must have a finally block if there is more than one catch block.
True
False
2.5 points
QUESTION 30
1. Assuming file data.txt exists and is successfully opened and contains the following text
CS1
What does the file data.txt contain after this code sequence is executed?
try
{
FileOutputStream fos = new FileOutputStream("data.txt ", false );
PrintWriter pw = new PrintWriter( fos );
pw.println( “Java Illuminated”);
pw.close( );
}
catch( IOException ioe )
{
ioe.printStackTrace( );
}
|
a. |
Java Illuminated |
|
b. |
CS1Java Illuminated |
|
c. |
CS1 |
|
d. |
Java Illuminated |
2.5 points
QUESTION 31
1. Given the following code for two service classes:
public abstract class A
{
public A(){}
public A(String x)
{
System.out.println(x + " Overloaded constructor for class A called\n");
}
}
public class B extends A
{
public B()
{
super("Hello");
}
public B(String i)
{
super(i);
}
}
And given the following code for the application class:
public class App
{
public static void main(String[] args)
{
B b = new B();
System.out.println("Hello Again ");
}
}
What would the output be when the application class is executed?
|
a. |
Nothing is output |
|
b. |
Hello Overloaded constructor for class A called |
|
c. |
Hello Again |
|
d. |
Overloaded constructor for class A called |
2.5 points
QUESTION 32
1. Java, multiple inheritance is implemented with the keyword implements using the concept of
|
a. |
An abstract class |
|
b. |
One direct superclass only |
|
c. |
An interface |
|
d. |
Multiple inheritance is not supported in Java |
2.5 points
QUESTION 33
1. A non abstract subclass extending an abstract superclass must provide the implementation for all direct abstract superclass abstract methods.
True
False
2.5 points
QUESTION 34
1. For Question 34 consider the following three classes:
public class A
{
private int number;
protected String name;
public double price;
public A()
{
System.out.println( "A() called");
}
private void foo1()
{
System.out.println( "A version of foo1() called");
}
protected int foo2()
{
System.out.println( "A version of foo2() called");
return number;
}
public String foo3()
{
System.out.println( "A version of foo3() called");
return "Hi";
}
}
public class B extends A
{
private char service;
public B()
{
super();
System.out.println( "B() called" );
}
public void foo1()
{
System.out.println( "B version of foo1() called");
}
protected int foo2()
{
int n = super.foo2();
System.out.println( "B version of foo2() called");
return ( n + 5 );
}
public String foo3()
{
String temp = super.foo3();
System.out.println( "B version of foo3() called");
return ( temp + " foo3");
}
}
public class C extends B
{
public C()
{
super();
System.out.println( "C() called");
}
public void foo1()
{
System.out.println( "C version of foo1() called");
}
}
What is the output of the following code sequence:
B b2 = new B();
b2.foo1();
|
a. |
A() called |
|
b. |
A() called |
|
c. |
A() called |
|
d. |
A() called |
2.5 points
QUESTION 35
1. For Question 35 consider the following three classes:
public class A
{
private int number;
protected String name;
public double price;
public A()
{
System.out.println( "A() called");
}
private void foo1()
{
System.out.println( "A version of foo1() called");
}
protected int foo2()
{
System.out.println( "A version of foo2() called");
return number;
}
public String foo3()
{
System.out.println( "A version of foo3() called");
return "Hi";
}
}
public class B extends A
{
private char service;
public B()
{
super();
System.out.println( "B() called" );
}
public void foo1()
{
System.out.println( "B version of foo1() called");
}
protected int foo2()
{
int n = super.foo2();
System.out.println( "B version of foo2() called");
return ( n + 5 );
}
public String foo3()
{
String temp = super.foo3();
System.out.println( "B version of foo3() called");
return ( temp + " foo3");
}
}
public class C extends B
{
public C()
{
super();
System.out.println( "C() called");
}
public void foo1()
{
System.out.println( "C version of foo1() called");
}
}
What is the output of the following code sequence:
B b3 = new B();
int n = b3.foo2();
|
a. |
A() called |
|
b. |
A() called |
|
c. |
A() called |
|
d. |
B() called |
2.5 points
QUESTION 36
1. For Question 36 consider the following three classes:
public class A
{
private int number;
protected String name;
public double price;
public A()
{
System.out.println( "A() called");
}
private void foo1()
{
System.out.println( "A version of foo1() called");
}
protected int foo2()
{
System.out.println( "A version of foo2() called");
return number;
}
public String foo3()
{
System.out.println( "A version of foo3() called");
return "Hi";
}
}
public class B extends A
{
private char service;
public B()
{
super();
System.out.println( "B() called" );
}
public void foo1()
{
System.out.println( "B version of foo1() called");
}
protected int foo2()
{
int n = super.foo2();
System.out.println( "B version of foo2() called");
return ( n + 5 );
}
public String foo3()
{
String temp = super.foo3();
System.out.println( "B version of foo3() called");
return ( temp + " foo3");
}
}
public class C extends B
{
public C()
{
super();
System.out.println( "C() called");
}
public void foo1()
{
System.out.println( "C version of foo1() called");
}
}
What is the output of the following code sequence:
B b4 = new B();
System.out.println( b4.foo3() );
|
a. |
A() called |
|
b. |
A() called |
|
c. |
A() called |
|
d. |
A() called |
2.5 points
QUESTION 37
1. True or False, a method from a subclass that overrides a superclass method having the same method signature cannot call that superclass method from within the overriding subclass method body.
True
False
2.5 points
QUESTION 38
1. For Question 38 consider the following two classes:
public abstract class C
{
private void foo1()
{
System.out.println( “Hello foo1” );
}
public abstract void foo2();
public abstract int foo3();
public void foo1Call()
{
foo1();
}
}
public class D extends C
{
public void foo2()
{
System.out.println( “Hello foo2” );
}
public int foo3()
{
return 10;
}
private void foo4()
{
System.out.println( “Hello D foo4()” );
}
}
To instantiate an object of class C we could use the following statement(s):
|
a. |
C c2; |
|
b. |
C c2 = new C(); |
|
c. |
a or b |
|
d. |
none of the above |
2.5 points
QUESTION 39
1. For Question 39 consider the following two classes:
public abstract class C
{
private void foo1()
{
System.out.println( “Hello foo1” );
}
public abstract void foo2();
public abstract int foo3();
public void foo1Call()
{
foo1();
}
}
public class D extends C
{
public void foo2()
{
System.out.println( “Hello foo2” );
}
public int foo3()
{
return 10;
}
private void foo4()
{
System.out.println( “Hello D foo4()” );
}
}
Which of the following code sequences, if any, will successfully access private method foo1 in class C?
|
a. |
C c2 = new C(); |
|
b. |
C c2 ; |
|
c. |
D d1 = new D(); |
|
d. |
C c1 = new D(); |
|
e. |
None of the above |
2.5 points
QUESTION 40
1. Assuming a StringIndexOutOfBoundsException exception class exists and is a subclass of IndexOutofBoundsException, what is the output of this code sequence?
try
{
String word = new String("avaJ");
System.out.println( word.charAt( 3 ) );
}
catch( StringIndexOutOfBoundsException e )
{
System.out.println( “OOPS!\n” );
}
catch( IndexOutOfBoundsException ie )
{
System.out.println( ie.getMessage() );
}
finally()
{
System.out.println("I’d rather be sailing\n");
}
|
a. |
OOPS! |
|
b. |
J |
|
c. |
OOPS! |
|
d. |
J |
2.5 points
Click Save and Submit to save and submit. Click Save All Answers to save all answers.
-----------