Codingassignmenthelper | Home Codingassignmenthelper | University

CSC72003 Sample Exam 1

Assignment :

The interface for the String class from the Java documentation is attached at the end of this paper. Please detach and use when needed through all parts of this paper.

Part A: Short Answer Questions (60 marks). Answer all questions.


1. What is a keyword? Give two examples, using Java as the programming language. (2 marks)

2. What will be the output for the following program segment? (2 marks)
System.out.println(10%5);
System.out.println(10/5);
System.out.println(10<10);
System.out.println(10==10);

3. What is the difference between a constructor and a class method? (2 marks)

4. Create a class called DoSomething, that has two integer fields, int1 and int2. Initialise these fields in a constructor. (3 marks)

5. Rewrite the following “if” construct as a switch/case statement : (2 marks) char code;
if (code ==’A’) {
 System.out.println(“Excellent”);
 } else if (code ==’B’ || code == ‘C’) {
 System.out.println(“Good”);
 } else if (code == ‘D’) {
 System.out.println(“Poor”);
 }


6. Convert the following code segment into an equivalent for loop (2 marks)
public void display()
{
 int i = 10;
 while (i !=20)
 {
 System.out.println(i + “ “);
 i++;
 }constructor
}

7. What will be the output of the following program segment? (2 marks)
int a = 10;
int b = 30;
int c = 40;
a = b-- + c - a % 2;
System.out.println(“a”+a);
System.out.println(“b”+b);

8. What is the difference between a field and a local variable? (2 marks)

9. What is a Wrapper class? Give two examples of a Wrapper class (2 marks)

10. What is the purpose of a break statement? (2 marks)

11. For each of the Java method signatures below, answer the following questions: What is the method name? Does the method return a value? If yes, what is the type of the return value? How many parameters does the method have? Write a sample Java method call for the method signature; that is: how would you call the method in your code?
public void changeColour(String colour) (2 marks)
public int calculateLargest(int A, int B) (2 marks)
public boolean hasEaten() (2 marks)

12. Write a constructor for the Date class given the following sample object creation code (2 marks):
new Date(“March”, 30, 1932);
  Write another constructor that takes no parameters, for the same class (2 marks).

13. Explain the difference between accessor and mutator methods (2 marks).

14. What is the difference between the following statements? (1 mark)
System.out.println(“Hello”);

//and

System.out.print(“Hello”);

15. Why are method stubs used when coding classes and projects? (2 marks)

16. Create a truth table to help interpret the possible results of the following boolean expression. Assume a, b and c are boolean variables. (2 marks)
if ((a & b) || c) {
 // do something
}

17. Given the following class diagram, explain the relationship between the three classes (2 marks):
Image1

18. Write a declaration for a new ArrayList of Person objects, called students. Initialise the new ArrayList. Note: you may leave it empty. (2 marks)

19. What sort of loop is most commonly used to iterate through collections? (1 mark) If the Person class in Q18 has a method called “printName” that takes no parameters, and returns a String, write a loop that will print out all the students’ names, one per line. (2 marks).

20. What are the three main control structures in programming? (1 mark)

21. Name two ways of producing output for the user. Use examples to illustrate your answer. (2 marks).

22. When we create a method that reads from or prints to a file, we need to include the words “throws Exception” to the header. Why do we need to include these keywords? (2 marks)

23. Describe the meaning of cohesion, in terms of writing classes. Give an example of why cohesion is important (3 marks).

24. Briefly describe the concept of information hiding. (2 marks)

25. What is the difference between a method which is static and one which is not static? (1 mark)

Part B: Java Programming Problems. Choose 2 of the following problems. Each solution is worth 20 marks. You are expected to use comments in your code. Marks will be deducted for incorrect brackets, syntax etc, so be careful as possible (40 marks).

1. Write a program to enter any ten words from the keyboard, arrange them in alphabetical order (lexicographically) and print them in order in the terminal window.

2. Write a program in Java to accept a line of text from the user and create a new word formed out of the first letter of each word. For example, the input line “Mangoes are delivered after midnight” would produce “Madam”.

3. Write overloaded methods in Java to handle the following situation – a Shape class is created that has (at least one) method called calcArea(). Users should be able to use this method in the following way:
// assuming r is a Shape representing a rectangle, s is a Shape representing a square,
// and c is a Shape representing a circle:

System.out.println(“The area of the rectangle is “ + r.calcArea(6, 10);
System.out.println(“The area of the square is “ + s.calcArea(25);
System.out.println(“The area of the circle is “ + c.calcArea(4.5);

4. Write a program that inputs 10 numbers, and prints out the second largest number with a suitable message.

5. Write a program in java to print the following pattern.
 1
 1 5
 1 5 10
 1 5 10 15 20
(and so on) until the final number on a line is 50.

Part C: Optional extensions (max of 10 extra points, maximum overall of 100 marks)

1. Explain what the Clean and Build command does in the Netbeans environment (5 marks). Make sure you explain the result of this command as well as the mechanics of what it does.

2. What is the purpose of a code template in the Netbeans editor? What are their advantages and disadvantages? (5 marks)

To Continue Click Here > CSC72003 Sample Exam 1.pdf
Codingassignmenthelper | Home Codingassignmenthelper | Home