Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Programming: Test 1, Exercises of Java Programming

6 Unsolved Practice Questions.

Typology: Exercises

2021/2022

Uploaded on 02/24/2022

charlene
charlene 🇺🇸

4.8

(5)

266 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Family Name:.............................. Other Names: .............................
Student ID: ................................ Signature ..................................
Java Programming: Test 1
2016, Oct 10
Instructions
Time allowed: 90 minutes
Answer all the questions. There are 50 marks in total.
Write your answers in this test paper and hand in all sheets.
If you think some question is unclear, ask for clarification.
Brief Java documentation is provided with the test
This test contributes 10% of your final grade
(But your mark will be increased to your exam mark if that is higher.)
You may use dictionaries.
You may write notes and working on this paper, but make sure your answers are clear.
Questions Marks
1. Programs with variables [11]
2. Programs with text input and output [7]
3. Programs with Strings [6]
4. Programs with graphical output [12]
5. Programs with objects [9]
6. Programs with methods and parameters [5]
TOTAL:
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Java Programming: Test 1 and more Exercises Java Programming in PDF only on Docsity!

Family Name:.............................. Other Names:.............................

Student ID:................................ Signature..................................

Java Programming: Test 1

2016, Oct 10

Instructions

  • Time allowed: 90 minutes
  • Answer all the questions. There are 50 marks in total.
  • Write your answers in this test paper and hand in all sheets.
  • If you think some question is unclear, ask for clarification.
  • Brief Java documentation is provided with the test
  • This test contributes 10% of your final grade (But your mark will be increased to your exam mark if that is higher.)
  • You may use dictionaries.
  • You may write notes and working on this paper, but make sure your answers are clear.

Questions Marks

1. Programs with variables [11]

2. Programs with text input and output [7]

3. Programs with Strings [6]

4. Programs with graphical output [12]

5. Programs with objects [9]

6. Programs with methods and parameters [5]

TOTAL:

SPARE PAGE FOR EXTRA ANSWERS

Cross out rough working that you do not want marked. Specify the question number for work that you do want marked.

(b) [6 marks]

What will the following printValues method print out?

x:

y:

z:

public void printValues (){ double x = 50; double y = 20; double z = x + y; UI. println ("z is " + z);

x = y; y = x; UI. println ("x is " + x); UI. println ("y is " + y);

z = z + y; UI. println ("z is " + z);

z = x − z; UI. println ("z is " + z); }

z is

Question 2. Programs with text input and output [7 marks]

Complete the following calculateCylinder method that will ask the user for the height and radius of a cylinder and then print out the volume of the cylinder ( 2 π r^2 h).

You may use the value 3.14159 or the constant Math.PI in your method.

public void calculateCylinder (){

Question 4. Programs with graphical output [12 marks]

(a) [5 marks] Sketch what the following drawIt() method will draw in the graphics pane, if the user types 100 when asked for the size?

public static final double width = 50;

public void drawIt(){ double size = UI.askDouble("Enter the size:"); UI.drawRect(250, 150, width, size ); UI. fillOval (150, 50, size , width); UI.drawLine(width, size , width, 2 ∗ size ); }

  • MENU * 100 200 300 400

100

200

300

50

150

250

50 150 250 350 Enter the size: 100

Extra copy (in case you made a mistake):

  • MENU * 100 200 300 400

100

200

300

50

150

250

50 150 250 350 Enter the size: 100

(b) [7 marks]

Complete the following drawSign() method so that it draws the following sign in the UI window.

drawSign should

  • Ask the user for the center of the sign (x and y) and the diameter.
  • Compute values of the left, top, and right of the sign, and put them in variables.
  • Set the line width to be 1/10 of the diameter,
  • Draw the sign, using the variables.

public void drawSign(){

public void testTurtles (){

Question 6. Programs with Methods and Parameters [5 marks]

The following fancyTurtles() method makes a collection of TurtleRobots draw some shapes on the window. It uses the fancy(TurtleRobot t, double ang, double distance) method that is also given below.

In the box, sketch what fancyTurtles() will draw on the window. (You may use any empty space to draw it if you get it wrong the first time.

public void fancyTurtles (){ TurtleRobot t1 = new TurtleRobot(Color.blue); this. fancy(t1 , 45, 50); TurtleRobot t2 = new TurtleRobot(Color.red); this. fancy(t2 , −120, 20); this. fancy(t2 , 120, 40); this. fancy(t1 , 0, 40); this. fancy(t1 , −90, 10); }

public void fancy(TurtleRobot t , double ang, double dist ){ t .penOn(); t. turn(ang); t .move(dist); t. turn(ang); t .move(dist); t. turn(ang); t .move(dist); t. turn(− 3 ∗ang); t .penOff(); t .move(10); }

(Question 6 continued on next page)