Advertisements
Home > Information Technology, programming, Science > Oct 2009: Answer to Basic Programming 1st Quiz

Oct 2009: Answer to Basic Programming 1st Quiz

On Friday, October 9th 2009, the first quiz of basic programming was conducted by James Purnama. It was a 65 questions and 1 programming problem quiz. The topic covered in this quiz is from JAVA Concepts book chapter 1-4. The students can do an open book or search it in the web. Anyway, I thought I might share the quiz and the answer which I think is right to you guys for improving your programming knowledge. Use this answer at your own risk.

1. Consider the following statement:
String greeting = 13;
Which of the following is true?
Answer: B. The statement yields a compile-time error
Explanation: The assignment operator is mismatched since it
wants to store an integer to a string type reference.
Since this type of inconvertible error is detected during the
compile time, it will yield a compile-time error.

2. Which of the following statement is correct?
Answer: A. Identifiers can be made up of letters, digits, and
the underscore(_)characters.

3. List three rules imposed by Java on identifiers.
Answer: All true

4. It is an error to use the value of a variable that has never
had a value assigned to it.
Answer: A. True
Explanation: The compiler will say "The variable may not have
been initialized".

5. Which of the following code fragments will cause an error?
Answer:
D. int luckyNumber;
   System.out.println(luckyNumber);
Explanation: the variable "luckyNumber" was never initialized.

6. System.out is an object of the class .....
Answer: PrintStream

7. The ...... of a class specifies what you can do with its objects.
Answer: public interface

8. The System.out object belongs to the class.....
Answer: B. PrintStream

9. Which of the following statements is correct in the Java language?
Answer: C. Every object belongs to a class 

10. Which of the following counts the number of characters in a string?
String greeting = "Hello, World!";
Answer: A. int n = greeting.length();

11. Write a statement that sets the value of i to the number of characters
in the following string:
int i;
String message = "Go home early today.";
Answer: i = message.length();

12. A method name is ..... if a class has more than one method with that name
(but different parameter types).
Answer: overloading

13. In Java, numbers are objects of classes in the java.lang package.
Answer: B.False
Explanation: number is a primitive object which doesn't belong to a class.

14. Based on the code below, move the rectangle 25 units to the left and
40 units down.
Rectangle box = new Rectangle(5,10,20,30);
Answer: box.translate(-25,40);

15. An object can be referenced by at most one object variable.
Answer: B. False

16. Which of the following terms denotes the memory location of an object?
Answer: C. object reference

17. Object variables store .....
Answer: A. references

18. The acronym of AWT stands for ....
Answer: C. Abstract Windowing Toolkit

19. Complete this code fragment to ensure the frame is shown.
JFrame frame = new JFrame();
Answer: C. frame.setVisible(true);

20. Based on the code below, set the size of the frame to 50 pixels wide and
70 pixels tall.
JFrame frame = new JFrame();
Answer: frame.setSize(50,70);

21.Based on the following statement, which of the following statements sets 
the title of the frame?
Answer: D. frame.setTitle("An Empty Frame");

22.Complete this code fragment to ensure that the application exits properly 
when the user closes the frame.
JFrame frame = new JFrame();
Answer: D. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

23. Complete the following code fragment by writing two statements that 
would cause a program to display a square frame with a title that reads 
"My first GUI program".
JFrame frame = new JFrame();
final int FRAME_WIDTH = 400;
final int FRAME_HEIGHT = 400;
Answer:
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("My first GUI program");

24. Place drawing instruction inside the ... method. That method is 
called whenever the component needs to be repainted.
Answer: paintComponent

25. To draw an ellipse, you must include in your program the 
statement: import ...
Answer: java.awt.geom.Ellipse2D

26. The Graphics and Graphics2D class are part of the ... package.
Answer: AWT

27. When a window is shown the first time, the ... method is 
called automatically.
Answer: main

28. Use a(n) ... to recover the Graphics2D object from the Graphics 
parameter of the paintComponent method.
Answer: Cast

29. Whenever the Swing toolkit calls the paintComponent method, it 
actually passes a parameter of type ...
Answer: D. Graphics

30. ... is the nickname for the graphical user interface library 
in Java.
Answer: GUI

31. To run an applet, you need a(n) ... file with the applet tag.
Answer: HTML

32. You view applets with the ... or a Java enabled browser.
Answer: AppletViewer

33. Which of the following statements would complete the code below?
public class RectangleComponent extends JComponent
{ public void paintComponent(Graphics g)
  { //Recover Graphics2D
    Graphics2D g2 = (Graphics2D) g;
    //Draw a square
    ...
    g2.draw(box);
  }
}
Answer: A. Rectangle box = new Rectangle (5,10,20,20);

34. Consider the following code fragment:
public class RedFlagComponent
{

    public void paintComponent(Graphics g)
    {
          Graphics2D g2 = (Graphics2D) g;
          Rectangle flag = new Rectangle(100, 100, 200, 100);
          g2.draw(flag);
    } 

}
Answer: D. It is impossible to add a RedFlagComponent object to a 
frame because the class does not extend JComponent.

35. Complete the code below:
public void paint(Graphics g)
{
  //prepare for extended graphics
  Graphics2D g2 = (Graphics 2D) g;

  //Construct a rectangle and draw it
  Rectangle box = new Rectangle(5,10,20,30);
  g2.draw(box);

  //Move the rectangle 15 units to the right and 25 units down
  ...
  g2.draw(box);
}
Answer: box.translate(15,25);

36. The ... method of the Graphics2D class is used to draw a 
string anywhere in a component.
Answer: drawString

37. In Java, colors are specified by defining their ..., ..., 
and ... values.
Answer: Red, Blue, Green

38. To draw a line in Java, you should use an object of the .... class.
Answer: Line2D

39. What are the RGB color of Color.RED?
Answer: C. 255,0,0

40. What are the RGB color of Color.BLACK?
Answer: D. 0,0,0

41. In the code below, write a statement that sets the graphic to green.
public class ItalianFlagComponent extends JComponent
{
  public void paintComponent(Graphics g)
  {
     Graphics2D g2 = (Graphics2D) g;
     Rectangle.Double leftRectangle = new Rectangle.Double(100,100,30,60);
     ...
  }
}
Answer: B. g2.setColow(Color.GREEN);

42. Complete the following statement, which construct an ellipse.
Ellipse2D.Double ellipse = new ...(x,y,width,height);
Answer: Ellipse2D.Double

43. Based on the following code, write four statements that draw 
a red square on a yellow background.
public class RectangleComponent extends JComponent
{  public void paintComponent(Graphics g)
   {
     Graphics2D g2 = (Graphics2D) g;
     ...
   }
}
Answer:
g2.setBackground(Color.YELLOW);
Rectangle r = new Rectangle(0,0,10,10);
g2.setColor(Color.RED);
g2.draw(r);

44. The programmers who designed and implemented the library classes 
such as PrintStream and Rectangle are called ...
Answer: A. System programmers

45. What is the missing code fragment in the following code?
public class CarComponent extends JComponent
{
   public void paintComponent(Graphics g)
   {
       Graphics2D g2 = (Graphics2D) g;
       Car car1 = new Car(0,0);
       int x = getWidth() - Car.WIDTH;
       int y = getHeight() - Car.HEIGHT;
       Car car2 = new Car(x,y);
       ...
   }
}
Answer: All Wrong
Explanation: Shouldn't the one that draws the component 
is the g2 variable instead of the Car component?

46. It is a good idea to define ... for each complex shape 
that you want to draw.
Answer: A separate class

47. Which of the following code fragments converts a floating-point 
number to the nearest integer?
Answer:
B. double f = 4.65;
   int n = (int) Math.round(f);
explanation: You need a cast type to integer since Math.round(double) returns a
long.

48. The decimal equivalent of 110100 is ...
Answer: C. 52

49. The binary equivalent of 1000(decimal) is ...
Answer: A. 1111101000

50. Which of the following primitive types is a floating-point 
type with a size of eight bytes?
Answer: B. double

51. Which of the following primitive types has a size of two bytes?
Answer: D. char

52. Which of the following code fragments will compile without error?
Answer:
B. int dollars = 100;
   double balance = dollars;
Explanation: Integer has less bytes than double. Therefore the 
system still works to store a less bytes to a bigger bytes 
variable storage.

53. Consider the code fragment.
double balance = 13.75;
int dollars = balance;
Which of the following is true?
Answer: D. The code doesn't compile
Explanation: Double has bigger bytes than integer. 
Therefore the system can't store a bigger bytes to 
a less bytes variable storage.

54. The binary equivalent of 200 is ...
Answer: D. 11001000

55. The decimal equivalent of 111010 is ...
Answer: A. 58

56. Which of the following correctly defines a constant 
in a method?
Answer: C. final double NICKEL_VALUE = 0.05;

57. Numerical ... are values that do not change and that 
have a special significance
for a computation.
Answer: constant 

58. Which of the following statements is equivalent to 
balance = balance + amount;?
Answer: A. balance += amount;

59. The value of x after the following sequence is ...
x--;x++;
Answer: The original value of x before the decrement or x+0

60. Consider this statement for computing the average of x,y, and z.
double average =  x + y + z / 3;
Which of the following statements is correct?
Answer: D. The code only gives the right answer when x = -y.
Explanation: To put it clear, the formula can be defined as x + y + (z /3).
Therefore, the computation computes the right average if the 
sum of x and y is 0.

61. A string is a sequence of ...
Answer: chars or characters

62. Convert the following string as price:
String input = "75.23";
Answer:
try {
  double price = Double.parseDouble(input);
}
catch(Exception e){} 

63. The ... method is used to extract a part of a string.
Answer: substring

64. The ... character is used as an escape character in Java.
Answer: \ or backslash

65. Java uses the ... encoding scheme to encode international characters.
Answer: Unicode
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: