Download File - algebrahd.org

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Sample - Final Exam – COSC 1315
A. Concepts (5 pts each)
1. Briefly explain the role of variables.
2. Briefly explain the role of assignment statements.
3. Briefly explain the role of conditional statements.
4. Briefly explain the role of iteration statements.
5. Briefly describe the concept of objects and classes.
6. Briefly explain the role of object variables and methods.
7. Briefly describe the role of parameters
8. Briefly explain the difference(s) between a void method and a non-void method.
B. Understanding (10 points each)
1. What are printed out from the Java statements below.
System.out.println( “x = “ + 14/3 );
System.out.println( “y = “ + 18%5 );
System.out.println( “z = “ + ( 4 * Math.pow( 3,2 ) + 1 ) );
2. What are printed out from the Java statements below.
int x = input.nextInt(); //get input from the keyboard
if ( x > 5 && x <= 10 )
x++;
else
x--;
System.out.println( “x = ” + x);
If the user type in
3 ? If the user type in
10 ? If the user type in
14 ?
3. What are printed out from the Java statements below.
int x = input.nextInt(); //get input from the keyboard
int y = 9;
if ( x < 5 || x >= 10 ){
x++;
y += x;
}
else
x--;
y++;
System.out.println( “x = ” + x + “ and y = “ + y);
If the user type in
3 ? If the user type in
10 ? If the user type in
14 ?
4. What are printed out from the Java statements below.
int x = input.nextInt(); //get input from the keyboard
switch(x+3){
case 5: x += 3; break;
case 8: x -= 2; break;
case 2: x *= 5; break;
default: x = 100;
}
System.out.println( “x = ” + x);
If the user type in
5 ? If the user type in
2?
5. What are printed out from the Java statements below.
int x = input.nextInt(); //get input from the keyboard
for (int j = x ; j > 0 ; j-- )
System.out.print( j + “ , “);
if the user type in
5?
6. What are printed out from the Java statements below.
int x = input.nextInt(); //get input from the keyboard
int count = 0, sum = 0;
while (count < x ){
sum += count;
count += 2;
}
System.out.print( sum );
If the user type in
10 ?
7. What are printed out from the Java statements below.
int x = input.nextInt(); //get input from the keyboard
int count = 2, sum = 0;
do{
sum += count;
count += 2;
} while (count < x );
System.out.print( sum );
If the user type in
7?
8. What are printed out from the Java statements below.
int x = input.nextInt(); //get input from the keyboard
for (int i = 0 ; i < x ; i++ )
for (int j = 0 ; j < i ; j++ )
System.out.print( (i + j ) + “ , “);
If the user type in
4?
Next question(s) involving the following class
public class MySquare{
private int width;
public MySquare (int wid) { width = wd; }
public void setWidth(int wd) { width = wd; }
public int getWidth( ) { return width; }
}
9. What are printed out from the Java statements below.
MySquare sq = new MySquare(6);
System.out.println (sq.getWidth());
sq.setWidth(4);
System.out.println (sq.getWidth());
sq.setWidth(2*sq.getWidth()- 3);
System.out.println (sq.getWidth());
C. Programming
1. ( 5pts ) Write Java statement(s) to declare a variable of datatype int , named age , then initialize
this variable to 25.
2. ( 5pts ) Write Java statement(s) to double the value of variable amount below.
double amount = input.nextDouble();
3. ( 10pts ) The coefficients of a quadratic equation are saved in double variables a , b, and c. One of
the solutions will be saved in variable named x. One solution of this equation is
x
 b  b 2  4ac
2a
Write a Java statement that implement this formula and save it in variable x.
4. ( 10pts ) Write a conditional statement to check if the value of a variable named sales is greater
than or equal to 100. Assign 0.1 (10%) to a variable named discountRate if this is true.
5. ( 10pts ) Write a conditional statement to check if the value of a variable named grade is greater
than or equal to 70 then assigns the value “pass” to a String variable named msg if this is true;
otherwise, assigns “fail” to variable msg.
10. ( 10pts ) Write a for loop that prints out all odd numbers between 3 and 151 inclusively.
11. ( 10pts ) Write a while loop that prints out all multiples of 3 between 3 and 151 inclusively.
12. ( 10pts ) Write a nested loop that prints out a 5 rows by 7 columns rectangle made of asterisks *.
13. ( 5pts ) Write the header of a Java class named MyRect.
IN THE CLASS MyRect
WRITE THE FOLLOWING
14. ( 5pts ) Write the header of a constructor of the class MyRect. This constructor has THREE
parameters: one is of String datatype and named id and two others are of double datatype and
named wid and hei
15. ( 5pts ) Write statement(s) to declare instance (object) variables of the class MyRect.
width and height : double
id : String
16. ( 5pts ) Write the header of an accessor method that returns the id of a MyRect object.
17. ( 5pts ) Write the header of a mutator method that allows users to change the value of variable
width.
18. ( 10 PTS ) Write the header of a method named isASquare that checks if an object of type
MyRect is a square or not ( height and width have the same value). This method returns true if
this object is a square; returns false otherwise. Implement the method (write code).
IN THE main METHOD OF CLASS TestMyRect
WRITE THE FOLLOWING
22. ( 5 pts ) Write a statement that instantiate (create) an object of datatype MyRect
Initialize any valid values.
23. ( 5 pts ) Write statement(s) to prints out the id of an object of type MyRect named myRect
24. ( 5 pts ) Write a statement set the value width of myRect to 25
25. ( 10 pts ) Write statement(s) print out if myRect is a square or not