1 / 5100%
Last Name:________________ First Name ________
Q1. Once we have implemented the solution, we are not done with the problem because
a) the solution may not be the best (most efficient)
b) the solution may have errors and need testing and fixing before we are done
c) the solution may, at a later date, need revising to handle new specifications
d) the solution may, at a later date, need revising because of new programming
language features
e) all of the above
Q2. Which of the following would not be considered an algorithm?
a) a recipe
b) a computer program
c) pseudocode
d) a shopping list
e) travel directions
Q.3 Examine the code in each row. Decide if the printed output will be true or false.
Point pointA = new Point( );
Point pointB = new Point( 0, 0 );
System.out.println( pointA == pointB );
______________
Point pointA = new Point( 21, 17 );
Point pointB = pointA;
System.out.println( pointA == pointB );
___________
Point pointA = new Point( 21, 17 );
Point pointB = new Point( 21, 17 );
System.out.println( pointA == pointB );
___________
The type char is a primitive data type _________________
The type String is a primitive data type ________________
Q.4 Having multiple class methods of the same name where each method has a different
number of or
type of parameters is known as
a) encapsulation
b) information hiding
c) tokenizing
d) importing
e) method overloading
Exam 2 Review Questions
Q.5. Correct the following class definition if you think it will not work:
public class Student
{
private String name;
private String major;
public Student()
{
name = “???”;
major=”xxx”;
}
public Student(String n,String m)
{
n = name;
m = major;
}
public String getMajor()
{
return m;
}
public String getName()
{
return n;
}
}
Q.6. To define a constant called MAX_MARKS in the class which of the following
statement is correct:
A.. public final int MAX_MARKS = 100;
B. public static int MAX_MARKS = 100;
C. public static final MAX_MARKS = 100;
D. public static final int MAX_MARKS = 100;
Ans:
Q.7 if k holds a value of the type int, then the value of the expression...
k <= 10 || k > 10
a) must be true c) could be either true or false
b) must be false d) is a value of type int
Ans:
Q.8 Write a method called median that receives in three integers and returns the median
value?
public int median(int num1, int num2, int num3)
Q.9. What will be the output of the following loops ? Indicate the number of times the output will be
displayed if it is too many to list.
int n = 979;
for( int j = 0; j <= n; j++ )
{
System.out.print( "Hello " );
}
int n = 5;
for( int j = 1; j <= n; j+=3 )
{
System.out.print( "Hello " );
int k=j;
while (k < n)
{
System.out.println(“Good Morning”);
k++;
}
j--;
}
int j = 1;
int n = 5;
while( j <= n )
{
System.out.print( "Hello " );
n--;
}
int j = 1;
while( j <= 11 )
{
System.out.print( "Hello " );
j = j + 3;
}
What is the output of the following code fragment ?
String name;
int i;
boolean startWord;
name = "Richard M. Nixon";
startWord = true;
for (i = 0; i < name.length(); i++)
{
if (startWord)
System.out.println(name.charAt(i));
if (name.charAt(i) == ' ')
startWord = true;
else
startWord = false;
}
What is the value of n ?
n = 0;
j = 7;
if ((j!=0) && (n < 25))
{
n = 1;
if (j > 4)
{
n = 2;
}
else
{
n = 3;
}
}
else
{
n = 4;
if (j%2 >= 2)
{
n = 5;
}
else
{
n = 6;
}
}
Q. 10. Complete the table given below. The first row has been filled in as an example. Initial values of
x and y are x= -2 and y=3.
Initial
Values
Statement
(s)
Value of x after
statement(s)
Value of y after
statement(s)
x= -2;
y= 3;
x++;
-1
3
x= -2;
y= 3;
x--;
y++;
x= -2;
y= 3;
x+=4;
y*=6;
x= -2;
y= 3;
y=y%2;
Q.11. Write the values (true or false) that the following expressions evaluate to. Assume that
x= -2 and y=3
i. x<=y
ii. (x<0)||(y<0)
iii.(x<=y) && (x < 0)
iv. ((x+y)>0) && !(y>0)
Q.12. Consider the following code segment.
char grade;
switch (grade)
{
case ‘A’: System.out.println(“Excellent!”);
case ‘B’: System.out.println(“Great Job!”);
break;
case ‘C’: System.out.println(“Average”);
case ‘D’: System.out.println(“Poor”);
case ‘E’: System.out.println(“Failed”);
}
a. What will be the output when grade = ‘A’ ?
b. What will be the output when grade = ‘B’ ?
c. What will be the output when grade = ‘C’ ?
Q13. Underline the valid method headings assuming they exist inside a class.
public void f( ) public integer String( int n )
public void main( String args ) public double void f2( )
private RationalNumber() public BankAccount bankAccount( )
public double sum( int left, right ) public void functionOne( int a, int b )
Q 14. For next two questions, write the requested portions of a class called BaseballPlayer. This class
contains the following data instances:
private String name;
private String position;
private int numAtBats;
private int numSingles;
private int numDoubles;
private int numTriples;
private int numHomeRuns;
private double battingAverage;
a) Write the constructor, which is passed the player’s name and position.
b) Write a toString method that returns the player’s name, position and batting average formatted to have 1
digit to the left of the decimal and 3 digits to the right of the decimal. Assume DecimalFormat has been
imported.
Students also viewed