test2Ans

advertisement
Programming_test2 ANS
Q1. Write a Java statement or a set of Java statements to accomplish each of the
following:
a. Print the message "This is a Java program" on two lines on two lines
in the command window where the first line ends with Java. Use only one
statement.
System.out.println( "This is a Java\nprogram" );
b. Sum the odd integers between 1 and 99 using a for structure. Assume the
integer variables sum and count have been declared.
sum = 0;
for ( count = 1; count <= 99; count += 2 )
sum += count;
Q2. Determine the values of each variable after the calculation is performed. Assume
that when each statement begins executing all variables have the integer value 5.
a) product *=x++;
b) quotient /= ++x;
Answer:
a) product = 25 , x = 6;
b) quotient = 0 , x = 6;
Q3. Give the method header for each of the following methods.
a. Method hypotenuse, which takes two double-precision, floating-point
arguments side1 and side2 and returns a double-precision, floating-point
result.
b. Method smallest, which takes three integers, x, y, z and returns an integer.
c. Method instructions, which does not take any arguments and does not
return a value.
d. Method intToFloat, which takes an integer argument, number and
returns a floating-point result.
a. double hypotenuse( double side1, double side2 )
b. int smallest( int x, int y, int z )
c. void instructions()
d. float intToFloat( int number )
Q4. Find the error in each of the following program segments and explain how the
error can be corrected:
a. int g() {
System.out.println( "Inside method g" );
int h() {
System.out.println( "Inside method h" );
}
}
Error: Method h is defined in method g, both methods return int
Correction: Move the definition of h out of the definition of g, change methods
return to void
b. int sum( int x, int y ) {
int result;
result = x + y;
1
}
Error: The method is supposed to return an integer, but does not.
Correction: Delete variable result and place the following statement in the
method:
return x + y;
or add the following statement at the end of the method body:
return result;
c. void f( float a ); {
float a;
System.out.println( a );
}
Error: The semicolon after the right parenthesis that encloses the parameter list
and redefining the parameter a in the method definition are each incorrect.
Correction: Delete the semicolon after (float a) and delete the declaration
float a; inside the method
d. void product() {
int a = 6, b = 5, c = 4, result;
result = a * b * c;
System.out.println( "Result is " + result );
return result;
}
Error: The method returns a value when it is not supposed to
Correction: Change the return type to int or delete statement return
result;
Q5. Answer the following questions regarding an array called fractions.
a. Define a constant variable ARRAY_SIZE initialized to 10.
b. Declare an array with ARRAY_SIZE elements of type float and initialize
the elements to 0.
c. Name the fourth element of the array.
d. Refer to array element 4.
e. Assign the value 1.667 to array element 9.
f. Assign the value 3.333 to the seventh element of the array.
a. final int ARRAY_SIZE = 10;
b. float fractions[] = new float[ ARRAY_SIZE ];
c. fractions[ 3 ]
d. fractions[ 4 ]
e. fractions[ 9 ] = 1.667;
f. fractions[ 6 ] = 3.333;
Q6. Answer the following questions regarding an array called table.
a. Declare and create the array as an integer array and with 3 rows and 3 columns.
(Assume the constant variable ARRAY_SIZE has been defined to be 3.)
b. How many elements does the array contain?
c. Use a for repetition structure to initialize each element of the array to the
sum of its subscripts. Assume the integer variables x and y are declared as
control variables.
a. int table[][] = new int[ ARRAY_SIZE ][ ARRAY_SIZE ];
b. Nine.
2
c. for ( int x = 0; x < table.length; x++ )
for ( int y = 0; y < table[ x ].length; y++ )
table[ x ][ y ] = x + y;
Q7.
Write a Java program with a class named Conversion that creates the GUI shown in
Figure, to convert lowercase letters to uppercase letters after entering string into the left
textbox.
Figure : Graphic User Interface for the conversion program
Part of the program has done for you.
(a) What suitable packages should be imported for writing the program.
Answer:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
(3marks)
(b) Complete the following items from (i) to (vii) in order to complete your codes.
public class LetterConv extends // (i) JFrame
{
private JLabel inStrLabel;
private JLabel outLabel;
private JTextField inStrTF;
private JTextField outStrTF;
private InHandler inHandler;
(1marks)
private static final int WIDTH = 500;
private static final int HEIGHT = 100;
public LetterConv ()
{
/*(ii)
*/
(3marks)
setTitle("CAPITAL LETTERS");
Container c = getContentPane();
c.setLayout(new GridLayout(1,4));
/*(iii)
*/
(4marks)
inStrLabel = new JLabel("Enter a string : ", SwingConstants.RIGHT);
outLabel = new JLabel("String in uppercase :", SwingConstants.RIGHT);
inStrTF = new JTextField(10);
3
outStrTF = new JTextField(10);
//(iv)
(4marks)
c.add(inStrLabel);
c.add(inStrTF);
c.add(outLabel);
c.add(outStrTF);
inHandler = new InHandler();
inStrTF.addActionListener(inHandler);
setSize(WIDTH,HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private class InHandler implements // (v) ActionListener
(1mark)
{
public void
/* (vi) actionPerformed */ (ActionEvent e)
(1mark)
{
String str;
//(vii)
(3 marks)
str = inStrTF.getText();
outStrTF.setText(str.toUpperCase());
}
}
public static void main( String args[] ) {
Conversion strUpper = new Conversion ();
}
}
4
Download