2017 Final Exam 2 Flashcards
public class Student{
//counts number of student object created
private static int counter = 0;
private int studentID;
private String firstName;
private String lastName;
private int age;
private int[] marks;
public Student(String fname, String lname){ counter++; studentID = counter; //some code goes here... } // some code goes here...
//The method below computes the average of all the marks in the student
//array
public double computeAverage(){
//some code goes here…
}
//some code goes here…
}
public class TestStudent{
public static main(String[] args){ //some java code goes here… } }
a) Initialize first name and last name in the constructor [2]
- this.fname = fname;
- this lname = lname;
b) Write a setter method for age data field [2]
public void setAge(int age){
this.age=age;
}
c) Write a setter method that initializes marks data field of type int[] with an array that exists elsewhere [3]
d)Write getter methods for age, first name and last name [3]
e)Write the code that computes the average of marks in the student array[4]
f)Write a method named findHighest, for finding the highest student mark in the array of marks. [4]
g) Conceptually, what is the difference between counter and studentID data fields?
By referring to the Tester class, answer the following;
h) Write code snippet that creates a student object and initializes first name, last name and age [3]
a) Explain the importance of a constructor in the java program. [2]
b) There are two forms of inheritance relationships: Has-A and IS-A
Differentiate the two in the following manner;
i. By definition [4]
ii. By using an example of a java code of three class: Animal, Dog and Feet. [6]
c) Consider the class below and answer the following questions;
public class MyClass{
//some code goes here… //given a string and a number, this method finds the //the character in a string, whose index is indicated //by number public static char findChar (String word, int number){ //some code here... } }
(i) Using one java instruction, show how you would call method, findChar, in a different class [3]
char myChar =MyClass.findChar(“Thursday”, 5);
1 mark for using assignment statement
1 mark for using class name when calling the method
1 mark for a declared char variable that receives returned string
b) What is the difference between a constructor and a method? [2]
Constructors initialize objects when they are created using the new keyword. They have no return type and share the same name as the class.
Methods define actions or behaviors that objects can perform. They can have a return type (like void or int) and any name that follows naming conventions.
c) What is the purpose of garbage collection in Java, and when is it used? [3]
d) What is static in java? [2]
e) What is an infinite loop? [2]
It is a loop that never comes to an end when it is being executed
f) Write the code for an infinite loop. [3]
while(true){
System.out.println(“infinite loop”);
g) What are the three things which a programmer must consider when using a loop that should terminate at some point?
h) Which loop should you use in a situation where you want the loop to repeat until the Boolean expression is false, but the loop should execute at least once? [1]
Do-while
i) Why should you indent the statements in the body of a loop? [2]
To make the code readable
To differentiate the code that belong to the loop and the one that does not
i.In the Java programming language, all source code is first written in plain text files ending with the ______extension. [1]
.java
ii. The java compiler generates ___________ [1]
Machine code
Describe a valid assignment statements. [1]