Name:     ID: 
 
Email: 

Chap_Method/Classes_2022

True/False   10 points
Indicate whether the statement is true or false.
 

 1. 

Java methods can return only primitive types (int, double, boolean, etc).
 

 2. 

A constructor must have the same name as its class.
 

 3. 

A constructor must always return an int.
 

 4. 

A class may contain methods but not variable declarations.
 

 5. 

Every class definition must include a constructor.
 

Multiple Choice  60 points
Identify the choice that best completes the statement or answers the question.
 

 6. 

Consider the following class declaration.
public class Sample
{
   private int a;
   private double b;

    public Sample(int x, double y)
    {
      a = x;
      b = y;
    }
     // No other constructors

   }

   // The following method appears in a class other than Sample.
    public static void test()
    {
        Sample object = new /* missing constructor call */ ;
    }

Which of the following could be used to replace /* missing constructor call */ so that the method will compile
without error?
a.
Sample()
b.
Sample(int x = 10, double y = 6.2)
c.
Sample(int x, double y)
d.
Sample(10, 6.2)
e.
Sample(6.2, 10)
 

 7. 

Consider the following class declaration

public class SomeClass

{
  private int num;
  public SomeClass(int n)
  {
    num = n;
  }
  public void increment(int more)
  {
    num = num + more;
  }
public int getNum()
  {
    return num;
  }
}

The following code segment appears in another class.
SomeClass one = new SomeClass(100);
SomeClass two = new SomeClass(100);
SomeClass three = one;
one.increment(200);
System.out.println(one.getNum() + " " + two.getNum() + " " + three.getNum());
What is printed as a result of executing the code segment?
a.
100 100 100
b.
300 100 100
c.
300 100 300
d.
300 300 100
e.
300 300 300
 

 8. 


Consider the definition of the Person class below. The class uses the instance variable adult to indicate whether a person is an adult or not.
public class Person
{
private String name;
private int age;
private boolean adult;
public Person (String n, int a)
{
name = n;
age = a;
if (age >= 18)
{
adult = true;
}
else
{
  adult = false;
}
}
}

Which of the following statements will create a Person object that represents an adult person?
a.
Person p = new Person ("Homer", "adult");
b.
Person p = new Person ("Homer", 23);
c.
Person p = new Person ("Homer", "23");
d.
Person p = new Person ("Homer", true);
e.
Person p = new Person ("Homer", 17);
 

 9. 


public class Student
{
      private String name;
      private String major;
      private double gpa;
      private int hours;

      public Student(String newName, String newMajor, double newGPA, int newHours)
      {
            name = newName;
            major = newMajor;
            gpa = newGPA;
            hours = newHours;
      }

      public String toString( )
      {
         
          return name + "\n" + major + "\n" + gpa + "\n" + hours
      }
}

Which of the following could be used to instantiate a new Student s1?
a.
Student s1 = new Student( );
b.
s1 = new Student( );
c.
Student s1 = new Student("Jane Doe",  "Computer Science",  3.333,   33);
d.
new Student s1 = ("Jane Doe", "Computer Science", 3.333, 33);
e.
new Student(s1);
 

 10. 


      Assume that another method has been defined that will compute and return the student’s class rank (Freshman, Sophomore, etc).  It is defined as:
public String getClassRank( )
      Given that s1 is a student, which of the following would properly be used to get s1’s class rank?
a.
s1 = getClassRank( );
b.
s1.toString( );
c.
s1.getHours( );
d.
s1.getClassRank( );
e.
getClassRank(s1);
 

 11. 

      What does the following code compute?
      int num = 0;
      for(int j = 0; j < 1000; j++)
      {
      c.flip( );
      if(c.isHeads())
                num++;
      }
      double value = (double) num / 1000;
a.
the number of Heads flipped out of 1000 flips
b.
the number of Heads flipped in a row out of 1000 flips
c.
the percentage of heads flipped out of 1000 flips
d.
the percentage of times neither Heads nor Tails were flipped out of 1000 flips
e.
nothing at all
 

 12. 


Use the following information to answer questions   The Die class from chapter 4 has two constructors defined as follows. Assume MIN_FACES is an int equal to 6.


public Die( )                         public Die(int faces)
{                              {
      numFaces = 6;                        if(faces < MIN_FACES) numFaces =6;
      faceValue = 1;                                                else   numFaces = faces;      
                                              faceValue = 1;
}                              }


The instruction      Die d = new Die(10);    results in

a.
The Die d having numFaces = 6 and faceValue = 1
b.
The Die d having numFaces = 10 and faceValue = 1
c.
The Die d having numFaces = 10 and faceValue = 10
d.
The Die d having numFaces = 6 and faceValue = 10
e.
A syntax error
 

 13. 

Consider the following class declaration.
public class IntCell
{
  private int myStoredValue;
  // constructor not shown
public int getValue()
  {
    return myStoredValue;
  }
  public String toString ()
  {
    return "" + myStoredValue;
  }
}
Assume that the following declaration appears in a client class.
IntCell m = new IntCell();
Which of these statements can be used in the client class?
  I.      System.out.println(m.getValue());
II.      System.out.println(m.myStoredValue);
III.      System.out.println(m);
a.
I only
b.
I I only
c.
I I I only
d.
I and II
e.
I and III
 

 14. 


Consider a sequence of method invocations as follows: 
main calls m1,
             m1 calls m2,
                        m2 calls m3
                             and then m2 calls m4,
                                     m3 calls m5.
If m4 has just terminated, what method will resume execution?
a.
m1
b.
m2
c.
m3
d.
m4
e.
main
 

 15. 



Consider the following method.
public static int mystery(boolean a, boolean b, boolean c)
{
  int answer = 7;
  if (!a)
{
  answer += 1;
  }
if (b)
  {
  answer += 2;
  }
if (c)
{
   answer += 4;
}
  return answer;
}

Which of the following method calls will return the value 11 ?
a.
mystery(true, true, true)
b.
mystery(true, false, true)
c.
mystery(false, true, false)
d.
mystery(false, false, true)
e.
mystery(false, false, false)
 

 16. 


public class Swapper
      {
                           private int x;
            private String y;
            public int z;
           
            public Swapper(int a, String b, int c)
            {
                  x = a;
                  y = b;
                  z = c;
            }

            public String swap( )
            {
                  int temp = x;
                              x = z;
                              z = temp;
                  return y;
            }

            public String toString( )
            {
                  if (x < z)  
                                                 return y;
                  else
                                     return "" + x + z;
            }
      }


If the instruction
      Swapper s = new Swapper(0, "hello", 0);
     is executed followed by s.toString( );
    what value is returned from s.toString( )?
a.
"hello"
b.
"hello00"
c.
"00"
d.
"0"
e.
0
 

 17. 

If we have Swapper r = new Swapper (5, "no", 10); then
   r.swap( ); returns which of the following?
a.
nothing
b.
"no"
c.
"no510"
d.
"510"
e.
“15”     
 

 18. 


what does this method do

public int foo (int num1, int num2)
{
   int result = num1;

   if (num2 > num1)
      result = num2;
  
   return result;
}
a.
accepts two integer parameters and returns the first number
b.
accepts two integer parameters and returns the first and second number
c.
accepts two integer parameters and returns the second number
d.
accepts two integer parameters and returns the larger of the two.
e.
accepts two integer parameters and returns nothing
 

 19. 


Consider the following methods, which appear in the same class.
public void printSum(int x, double y)
{
System.out.println(x + y);
}
public void printProduct(double x, int y)
{
System.out.println(x * y);
}
Consider the following code segment, which appears in a method in the same class as printSum and printProduct.
int num1 = 5;
double num2 = 10.0;
printSum(num1, num2);
printProduct(num1, num2);
What, if anything, is printed as a result of executing the code segment?
a.
15
50
b.
15
50.0
c.
15.0
50
d.
15.0
50.0
e.
Nothing is printed because the code does not compile.
 

 20. 

Consider the following method.
public static String abMethod(String a, String b)
{
  int x = a.indexOf(b);
while (x >= 0)
{
  a = a.substring(0, x) + a.substring(x + b.length());
  x = a.indexOf(b);
}
  return a;
}

What, if anything, is returned by the method call abMethod("sing the song", "ng") ?
a.
"si"
b.
"si the so"
c.
"si the song"
d.
"sig the sog"
e.
Nothing is returned because a StringIndexOutOfBoundsException is thrown.
 

 21. 

Which of the following is a method having same name as that of it’s class?
a.
finializ
b.
delete
c.
class
d.
constructor
e.
void
 

 22. 

Which method can be defined only once in a program?
a.
Math()
b.
main method
c.
finalize method
d.
static method
e.
private method
 

 23. 

What is the output of this program?

class box
    {
        int width;
        int height;
        int length;
        int volume;
        void volume(int height, int length, int width)
        {
             volume = width*height*length;
        }
    }   
    class Prameterized_method
    {
        public static void main(String args[])
        {
            box obj = new box();
            obj.height = 1;
            obj.length = 5;
            obj.width = 5;
            obj.volume(3,2,1);
            System.out.println(obj.volume);       
        }
     }
a.
0
b.
18
c.
25
d.
45
e.
6
 

 24. 

What is the output of this program?

class equality
    {
        int x;
        int y;
        boolean isequal()
        {
            return(x == y); 
        }
    }   
    class Output
    {
        public static void main(String args[])
        {
            equality obj = new equality();
            obj.x = 5;
            obj.y = 15;
            System.out.println(obj.isequal());
        }
    }
a.
True
b.
False
c.
0
d.
Error
e.
20
 

 25. 

Consider the following class definition.
public class Student
{
  private int studentID;
  private int gradeLevel;
  private boolean honorRoll;

  public Student(int s, int g)
   {
    studentID = s;
    gradeLevel = g;
    honorRoll = false;
   }
  public Student(int s)
  {
   studentID = s;
   gradeLevel = 9;
   honorRoll = false;
  }
}
Which of the following code segments would successfully create a new Student object?

Student one = new Student(328564, 11);
Student two = new Student(238783);
int id = 392349;
int grade = 11;
Student three = new Student(id, grade);
a.
Ionly
b.
II only
c.
III  only
d.
I and II only
e.
I, II, and III
 

FRQ write method only
 

 26. 

  FRQ is 15 points  

Write a Method only
Write the method, called between, which is intended to take three integers values and return true if the value x is between lower and upper values inclusive, and false otherwise.
assume the following
// precondition:  lower <= upper
// postcondition: returns true if x is between lower and upper,inclusive; otherwise, returns false
 

 27. 


FRQ is 15 points  

      Write a
Method only that will instatiate two Dies, roll them 1000 times and return the number of times the sum is equal to  7 is rolled on the two Die.

This is the Die class for you to see it:
public class Die
{
  public  Die ()
   {
      numFaces = 6;
      faceValue = 1;
   }
  public int roll ()
   {
      faceValue = generator.nextInt(numFaces) + 1;
      return faceValue;
   }

   public int getFaceValue ()
   {
      return faceValue;
   }
  
}
 



 
         Start Over