Test 1 Solution_0405

advertisement
CIM/Prog. II
Test. 1
(Solution)
Q1.
Suppose a, b, and sum are int variables and c is a double variable. What value is assigned to each variable
after each statement executes? Suppose a=3, b=5, and c= 14.1.
sum = a + b + c;
c / = a;
b + = c-a;
a * = 2*b+c;
Q2.
If x = 5, y = 6, z = 4, and
possible, state the reason.
a. (x + z) % y
b. (x + y) % w
c. (y + w) % x
d. (x + y) * w
e. (x % y) % z
f. (y % z) % x
Values of a:
3
3
3
50
Values of b:
5
5
6
6
Values of c:
14.1
4.7
4.7
4.7
Values of sum:
22.1
22.1
22.1
22.1
w = 3.5, evaluate each of the following statements, if possible. If it is not
3
not possible
not possible
38.5
1
2
Q3.
Consider the following statements:
String str = “Going to the amusement park.”;
char ch;
int len;
int position;
a. What value is stored in ch by the following statement?
ch = str.charAt(0);
The value stored in ch is ‘G’.
b. What value is stored in ch by the following statement?
ch = str.charAt(0);
The value stored in ch is ‘h’.
c. What value is stored in len by the following statement?
len = str.length();
The value stored in len is 28.
d. What value is stored in position by the following statement?
position = str.indexOf(‘t’);
The value stored in position is 7.
Q4.
Suppose that str is a String variable. Write a Java statement that uses the operator new to instantiate the
object str and assign the string “Java Programming” to str.
Ans:
str = new String (“Java Programming”)
Q5
What is the output of the following Java code?
x=100;
y=200;
if(x>100 && y<=200)
System.out.println(x + “ ” + y + “ ” + (x+y));
Else
System.out.println(x + “ ” + y + “ ” + (2*x-y));
Ans: 100 200 0
Page 1 of 8
CIM/Prog. II
Test. 1
(Solution)
Q6.
Suppose that you have the following declaration:
int j = 0;
The output of the statement
if ((8>4 || (j++ ==7)))
System.out.println(“j =” +j);
is:
j=0
while the output of the statement
if ((8>4 | (j++ ==7)))
System.out.println(“j =” +j);
is:
j=1
Explain why?
Ans: The expression ((8 > 4) || (j++ == 7)) is evaluated using short circuit. Because 8 > 4 is true, the
expression (j++ == 7) is not evaluate. Therefore, the value of j stays 0. On the other hand, to evaluate
the expression ((8 > 4) | (j++ == 7)) no short circuit is used. Therefore, both the expressions (8 > 4)
and (j++ == 7)) are evaluated. The expression j++ increments the value j by 1.
Q7
In the following code, correct any errors that would prevent the program from compiling or running.
public class Errors
{
public static void main(String[] args) {
int a, b;
boolean found;
System.out.print("Enter the first integers: ");
a = Integer.parseInt(keyboard.readLine());
System.out.println();
System.out.print("Enter the second integers: ");
b = Integer.parseInt(keyboard.readLine());
if a > a*b && 10 < b
found = 2* a > b;
else
{
found = 2 * a < b;
if found
a = 3;
c = 15;
if b
{
b = 0;
a = 1;
}
}
}
}
Page 2 of 8
CIM/Prog. II
Test. 1
(Solution)
Ans
import java.io.*;
public class Errors{
static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
int a, b;
int c;
boolean found;
System.out.print("Enter the first integers: ");
a = Integer.parseInt(keyboard.readLine());
System.out.println();
System.out.print("Enter the second integers: ");
b = Integer.parseInt(keyboard.readLine());
if( a > a*b && 10 < b)
found = 2* a > b;
else
{
found = 2 * a < b;
if(found)
a = 3;
c = 15;
if(b > 0)
{
b = 0;
a = 1;
}
}
}
}
Q8
What is the output of the following program?
Public class Mystery{
Public static void main(String[] args){
Int x, y, z;
X=4;
Y=5;
Z=y+6;
Do{
System.out.print(z = “ ”);
Z= z +7;
}
While (((z-x)%4) != 0);
System.out.println();
}
}
Ans: 11 18 25
Q9
Rewrites the following program segment by using a while and a do … while loop to have the same
output.
J = 2;
Page 3 of 8
CIM/Prog. II
Test. 1
(Solution)
For(i = 1;i<=5;i++){
System.out.print(j + “ ”);
J = j+5;
}
System.out.println();
Ans
a.
j = 2;
i = 1;
while(i <= 5)
{
System.out.print(j + " ");
j = j + 5;
i++;
}
System.out.println();
b.
j = 2;
i = 1;
do
{
System.out.print(j + " ");
j = j + 5;
i++;
} while(i <= 5);
System.out.println();
Q10
By using the provided program shell to complete a program that can create the GUI shown as in the
below figure, to convert temperature form Fahrenheit to Centigrade and from Centigrade to Fahrenheit.
Given:
i. If the user enters the temperature in Fahrenheit, the formula for calculating the equivalent
temperature in Centigrade is:
centigrade = (5.0/9.0)*(Fahrenheit -32)
ii. If the user enters the temperature in Centigrade, the formula for calculating the equivalent
temperature in Fahrenheit is: Fahrenheit = 9.0/5.0 * centigrade +32
The given shell for the conversion program, and complete the empty spaces inside the program:
//Java program to convert temperature between Centigrade and Fahrenheit.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
public class TempConversion extends JFrame{
Page 4 of 8
CIM/Prog. II
Test. 1
(Solution)
private JLabel centigradeLabel;
private JLabel fahrenheitLabel;
private JTextField centigradeTF;
private JTextField fahrenheitTF;
private CentHandler centigradeHandler;
private FahrHandler fahrenheitHandler;
private static final int WIDTH = 500;
private static final int HEIGHT = 50;
private static final double FTOC = 5.0/9.0;
private static final double CTOF = 9.0/5.0;
private static final int OFFSET = 32;
// ratio from Fahrenheit to Centigrade
// ratio from Centigrade to Fahrenheit
public TempConversion() {
setTitle("Temperature Conversion");
centigradeLabel =
fahrenheitLabel =
centigradeTF =
fahrenheitTF =
(7);
(7);
c.add(
);
centigradeHandler = new CentHandler();
fahrenheitHandler = new FahrHandler();
centigradeTF.addActionListener(centigradeHandler);
fahrenheitTF.addActionListener(fahrenheitHandler);
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
private class CentHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
double centigrade, fahrenheit;
}
}
private class FahrHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
double centigrade, fahrenheit;
}
}
public static void main( String[] args) {
}
}
Ans:
Page 5 of 8
CIM/Prog. II
Test. 1
(Solution)
//Java program to convert temperature between Centigrade and Fahrenheit.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
public class TempConversion extends JFrame{
private JLabel centigradeLabel;
private JLabel fahrenheitLabel;
private JTextField centigradeTF;
private JTextField fahrenheitTF;
private CentHandler centigradeHandler;
private FahrHandler fahrenheitHandler;
private static final int WIDTH = 500;
private static final int HEIGHT = 50;
private static final double FTOC = 5.0/9.0; // ratio from Fahrenheit to Centigrade
private static final double CTOF = 9.0/5.0; // ratio from Centigrade to Fahrenheit
private static final int OFFSET = 32;
public TempConversion() {
setTitle("Temperature Conversion");
Container c = getContentPane();
c.setLayout(new GridLayout(1,4));
centigradeLabel = new JLabel("Temp in Centigrade: ",
SwingConstants.RIGHT);
fahrenheitLabel = new JLabel("Temp in Fahrenheit: ",
SwingConstants.RIGHT);
centigradeTF = new JTextField(7);
fahrenheitTF = new JTextField(7);
c.add(centigradeLabel);
c.add(centigradeTF);
c.add(fahrenheitLabel);
c.add(fahrenheitTF);
centigradeHandler = new CentHandler();
fahrenheitHandler = new FahrHandler();
centigradeTF.addActionListener(centigradeHandler);
fahrenheitTF.addActionListener(fahrenheitHandler);
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
private class CentHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
double centigrade, fahrenheit;
Page 6 of 8
CIM/Prog. II
Test. 1
(Solution)
DecimalFormat twoDigits = new DecimalFormat("0.00");
centigrade = Double.parseDouble(centigradeTF.getText());
fahrenheit = centigrade * CTOF + OFFSET;
fahrenheitTF.setText(""+ twoDigits.format(fahrenheit));
}
}
private class FahrHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
double centigrade, fahrenheit;
DecimalFormat twoDigits = new DecimalFormat("0.00");
fahrenheit = Double.parseDouble(fahrenheitTF.getText());
centigrade = (fahrenheit - OFFSET) * FTOC;
centigradeTF.setText(""+ twoDigits.format(centigrade));
}
}
public static void main( String[] args) {
TempConversion tempConv = new TempConversion();
}
}
Q11
In the following program, number the marked statements in left hand space provided to show the order in
which they will execute (the logical order of execution).
import java.io.*;
public class Test
{
static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
int num1, num2;
____ System.out.println("Please enter two integers "
+ "on separate lines";
____ num1 = Integer.parseInt(keyboard.readLine());
____ num2 = Integer.parseInt(keyboard.readLine());
____ func (num1, num2);
____ System.out.println("The two integers are " + num1 + ", " + num2);
}
public static void func (int val1, int val2)
{
int val3, val4;
____ val3 = val1 + val2;
____ val4 = val1 * val2;
____ System.out.println("The sum and product are " + val3 + " and " + val4);
}
Ans:
import java.io.*;
Page 7 of 8
CIM/Prog. II
Test. 1
(Solution)
public classTest {
static BufferedReader keyboard = new
BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
int num1, num2;
__1__ System.out.println("Please enter two integers "
+ "on separate lines";
__2__ num1 = Integer.parseInt(keyboard.readLine());
__3__ num2 = Integer.parseInt(keyboard.readLine());
__4__ func (num1, num2);
__8__ System.out.println("The two integers are " + num1 + ", " + num2);
}
public static void func (int val1, int val2){
int val3, val4;
__5__ val3 = val1 + val2;
__6__ val4 = val1 * val2;
__7__ System.out.println("The sum and product are " + val3 + " and " + val4);
}
Q12
Condider the following method:
Public static int mystery(int x, double y, char ch){
int u;
if(‘A’ <= ch && ch <+ ‘R’)
return (2*x +(int)(y));
else
return((int)(2*y)-x);
}
What is the output of the following Java statements?
a. System.out.println (mystery (5, 4.3, ‘B’));
b. System.out.println (mystery (4, 9.7, ‘v’));
c. System.out.println (2*mystery (6, 3.9, ‘D’));
Ans:
a. 14;
b. 15;
c. 30
** END OF TEST **
Page 8 of 8
Download