Midterm 2 - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Midterm 2

Description:

Math. default. base type. midterm 2. 5. True/False Questions ... and grade. midterm 2. 17. Programming Questions ... Practice: One More Example of Array. Background: ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 21
Provided by: rober854
Category:
Tags: grade | math | midterm | practice | th

less

Transcript and Presenter's Notes

Title: Midterm 2


1
Midterm 2
  • Dr. Hairong Zhao
  • hairong_at_calumet.purdue.edu
  • http//ems.calumet.purdue.edu/mcss/zhao
  • Purdue University Calumet

2
Summary
Total points 100
3
Fill in the Blank Questions
  • The ______________ operator is used to connect
    two strings to make one longer string.
  • The statement "System.out.println("\"Java\"")
    prints out _________ on the monitor.
  • The reserved word __________ can be used as a
    name for the calling object.
  • A __________ method is a method that can be
    invoked using the class name instead of an object
    name.
  • The wrapper class of char is __________

concatenation ()
"Java"
escape character
this
static
Character
4
Fill in the Blank Questions
  • The method used to convert from String to Float
    is __________.
  • A(n) __________ is a special kind of method used
    to initialize objects.
  • The __________ class provides the methods min and
    round, among others.
  • A constructor with no parameters is called a
    __________ constructor.
  • The type of elements in an array is called the
    __________.

parseFloat
constructor
Math
default
base type
5
True/False Questions
  • The object and dot can be omitted in a non-static
    method call if the calling object is the this
    parameter.
  • Instance variables cannot be referenced in the
    definition of any static method unless an object
    (other than this) and dot precede the variable
    name.
  • Java will use automatic type conversion before it
    will use overloading.
  • It is possible to have two methods in the same
    class that have the same name, the same number
    and types of arguments, but different return
    types.
  • Constructors have return type void.

6
True/False Questions
  • A class may have more than one constructor.
  • If you add at least one constructor to a class,
    no constructors will be automatically created for
    the class.
  • All data stored in an array must be the same
    type.
  • If one element from an array is passed as a
    parameter to a method, then the called method can
    modify any element in the array.
  • In a two-dimensional array, all of the rows must
    have the same number of elements.

7
Multiple-Choice Questions
  • Which of these words is the name of a wrapper
    class?
  • A. int B. float C. Double D. String
  • A(n) __________ allows you to have a class object
    that corresponds to a value of a primitive type.
  • A. wrapper class B. type cast C. constructor
  • D. static variable
  • The lowest index that can be used with an array
  • A. is zero
  • B. is one
  • C. is determined by the array declaration
  • D. changes as the program runs
  • Normally all instance variables are given the
    qualifier
  • A. public B. protected C.
    private D. static

8
Multiple-Choice Questions
  • The term overloaded refers to
  • A. an error where more than one method matches a
    method call
  • B. two or more methods in the same class that
    have the same name
  • C. method calls where there are two many actual
    parameters
  • D. two or more methods in different classes that
    have the same name
  • The name of a constructor
  • A. can be any legal identifier
  • B. must include the name of the package
  • C. will always be the word new
  • D. will always be the same as the name of the
    class

9
Multiple-Choice Questions
  • Java will automatically define a constructor
  • A. if the programmer does not define a default
    constructor for a class
  • B. if the programmer does not define any
    constructors for a class
  • C. if the program refers to a constructor with
    no parameters
  • D. for every class defined in a program
  • Which of the following is correct Java code?
  • A. int10 list
  • B. int list10
  • C. int list new int10
  • D. int list new int10

10
Multiple-Choice Questions
  • A private instance variable
  • A. can never be modified by a method outside the
    class
  • B. can be modified by a method outside the class
    if it is returned by a method
  • C. can be modified by a method outside the class
    using a mutator method (assuming one exists)
  • D. can be modified by using an object name
    followed by a dot and then the name of the
    instance variable
  • What will happen if a program uses an array index
    that is out of bounds?
  • A. The compiler will give an error message and
    will not compile the program.
  • B. The compiler will give a warning but will
    still compile the program.
  • C. The program will compile but Java will give
    an error message when the program runs.
  • D. The program will compile and run with no
    error messages but might give incorrect results.

11
Multiple-Choice Questions
  • Which statement best describes the following
    statement
  • String a new String10
  • A. 10 String objects will be created when the
    array is created.
  • B. 10 String objects must be created before the
    array is created.
  • C. The array will be created with the elements
    equal to null.
  • D. Elements of an array cannot be a class type.

12
Multiple-Choice Questions
  • What will be the value of w after the following
    section of code executes
  • int w 4, q 3
  • if (q gt 5)
  • if (w 7)
  • w 3
  • else
  • w 2
  • else
  • if (w gt 3)
  • w 1
  • else
  • w 0
  • A. 0 B. 1 C. 2 D. 3

13
Multiple-Choice Questions
  • What is the output of the following code?
  • int list 0, 5, 10, 15, 20
  • int j
  • for(j 1 j lt 5 j)
  • System.out.print(listj " ")
  • System.out.println()
  • a. 0 5 10 15 20 b. 5 10 15 20 0 c. 5 10
    15 20 20
  • d. Code contains index out-of-bound

14
Multiple-Choice Questions
  • Consider the following function
  • int strange(int list, int item) int
    count0 for(int j 0 j lt list.length
    j) if(listj item)
    count return countWhich
    of the following statements best describe the
    behaviour of this function?a. This function
    returns the number of values stored in list.b.
    This function returns the sum of all the values
    of list.c. This function returns the number of
    times item is stored in list.d. None of these


15
Multiple-Choice Questions
  • Given the following declaration
  • int alpha new int10 int beta new
    int 10
  • Which of the following is a valid call to
    the function Math.min?
  • a. Math.min(alpha0, alpha1)
  • b. Math.min(alpha, beta)
  • c. Math.min(alpha0, beta)
  • d. Math.min(alpha, beta0)

16
Programming Questions
  • 1. We are going to define a class called
    Student, which stores the students name and
    his or her score. The instance variables have
    been defined, along with two accessors, getName
    and getGrade. Your task is
  • (1) Define a constructor , including the heading
    and the body, to initialize the name and score as
    given by the parameter.
  • (2) Define a second constructor, including the
    heading and the body, to initialize the name as
    given by parameter and default the grade to be 0.
  • (3) Define a method to determine whether this
    student is equal to anotherStudent. The method
    returns true only if they have the same name
    (which is a string!) and grade.

17
Programming Questions
  • 2. Define a class called ArraySum, which has
    three static methods
  • (1) The first one computes the sum of
    one-dimensional-array of integer. The heading of
    the method is already given you need to write
    the code for the body of the method.
  • (2) The second one computes the sum of
    two-dimensional-array of integers. Here you need
    to write both the heading and the body of the
    method.
  • (3) The third one is the main method, which tests
    the defined method. You need to
  • create a two-dimensional-array c55
  • initialize the array such that cij ij
  • compute the sum of this array by calling the
    method you just defined
  • print out the sum

18
Practice One More Example of Array
  • Background
  • A prime number is a positive integer greater than
    1 that is only divisible by itself and by 1.
  • 0, 1 , 4, 6 are not prime numbers
  • 2, 3, 5, are prime numbers.
  • How to test if a number n is a prime number?
  • all you need to do is to test if ni0 for all
    i lt sqrt(n)
  • boolean isPrimetrue
  • for (int i 2 i lt Math.ceil(Math.sqrt(n))
    i)
  • if (ni0)
  • isPrime false
  • break

19
Practice One More Example of Array
  • Create a class PrimeNumbers
  • Instance variables
  • Boolean sieve // sievei is true if and
    only if i is a prime number
  • Public methods.
  • Static boolean isPrime(int n) returns a boolean
    value indicating whether integer n is prime.
  • a constructor that takes a single integer
    parameter, upper, specifying the upper bound of
    the numbers to be considered
  • you should initialize the array sieveupper 1
  • accessor methods
  • int getNumberPrimeNumbers(int n) - returns the
    number of prime numbers less than or equal to n.
  • int getNthPrime(int n) - returns the n-th prime
    number.
  • getUpper - returns sieve.length-1
  • String toString() returns a string which
    contains all the prime numbers less than or equal
    to upper

20
Practice One More Example of Array
  • Problem
  • for getNthPrime(int n), what if there are there
    are no n prime numbers in sieve
  • for getNumberPrimeNumbers(n), what if n gt
    sieve.length
  • Solution
  • Add a private method to PrimeNumbers called
    checkAndResize(int n).
  • The checkAndResize does nothing if n is in the
    sieve range (e.g., if n1 is less than or equal
    to the length of sieve). However, if n is too
    large for the sieve, resize the sieve with 2n
    elements and initialize them.
  • Modify the getNumberPrimeNumbers method to call
    checkAndResize with a parameter of n to correctly
    handle the situation when n is too big for the
    sieve.
  • Modify the getNthPrime to call checkAndResize.
Write a Comment
User Comments (0)
About PowerShow.com