PRG 420
CLICK HERE TO DOWNLOAD THIS ANSWER INSTANTLY $38 Only
PRG 420
Part I. Multiple Choices
What is the valid way to declare an integer variable named a ? (Check all that apply.)
int a;
a int;
integer a;
A constructor has the same name as the class name.
true
false
Given the following code declaring and initializing two int variables a and b with respective values 3 and 5, indicate whether the value of each expression is true or false.
int a = 8;
int b = 13;
Expression true false
a < b true ____
a != b true ____
a == 4 ____ false
(b - a )<= 1 ____ false
b <= 5 true ____
You can simulate a for loop with a while loop.
true
false
What are the valid ways to declare an integer array name a (check all that apply)
int [ ] a;
int a[ ];
array int a;
int array a;
An array a has 30 elements; what is the index of the last element?
30
31
29
A class is analogous to a
cookie
blue jazz
bakery
cookie cutter
This key word causes an object to be created in memory.
create
new
object
construct
String is a primitive data type in Java.
true
false.
It is legal to have more than one constructor in a given class.
true
false
Part II. Reading and Understanding Code
What is the output of this code sequence?
String s = “Ciao”;
s = s.toLowerCase();
System.out.println( s );
What is the output of this code sequence?
int grade = 80;
if (grade >= 90 )
System.out.println("A");
else if (grade >= 80 )
System.out.println("B");
else if (grade >= 70 )
System.out.println("C");
else
System.out.println("D or lower");
What are the values of i and product after this code sequence is executed?
int i = 6;
int product = 1;
do
{
product *= i;
i++;
} while ( i < 9 );
What is the value of i after this code sequence is executed?
int i = 0;
for (i = 0; i < 300; i++ )
System.out.println(“Good Morning!”);
What is the value of sum after this code sequence is executed?
int sum = 0;
for( int i = 10; i > 5; i-- )
sum += i;
Part III. Fill in the code (1 point each, 4 questions)
Example-1: Write the code to declare an integer variable named x and assign x the value 777.
Example-2: This code prints the String “Hello San Diego” in all capital case.
String s = “Hello San Diego”;
This code prints the number of characters in the String “Hello World”
String s = “Hello World”;
Write the code to declare a double variable named pi and assign pi the value 3.14.
This loop calculates the sum of the first five positive multiples of 3 using a while loop (the sum will be equal to 3 + 6 + 9 + 12 +15 = 45)
int sum = 0;
int countMultiplesOf3 = 0;
int count = 1;
Here is a while loop; write the equivalent for loop.
int i = 0;
while( i < 88 )
{
System.out.println(“Hi there”);
i++;
}
// your code goes here
Part IV. Identifying Errors in Code
(5 points: 1 point each questions)
Example: Where is the error in this code :
int a = 3.3;
You coded the following on line 8 of class Test.java:
int a = 3 // line 8
When you compile, you get the following message:
Test.java:8: “;” expected
int a = 3
Where is the error in this code sequence?
String s = “Hello World”;
system.out.println( s );
Where is the error in this code sequence?
String s = String(“Hello”);
System.out.println( s );
Where is the problem with this code sequence (although this code sequence does compile)?
int i = 0;
while (i < 3 )
System.out.println(“Hello”);
You coded the following in the class Test.java:
int i = 0;
for(int i = 0; i < 3; i++ ) // line 6
System.out.println(“Hello” );
At compile time, you get the following error:
Test.java:6: i is already defined in main( java.lang.String[] )
for( int i = 0; i < 3; i++ ) // line 6
^
1 error
Part V. Object Oriented Programming
(3 points: 1 point each questions)
Find the Error
Find the error in the following class.
public class MyClass
{
private int x;
private double y;
public void MyClass(int a, double b)
{
x = a;
y = b;
}
}
You coded the following on lines 10–12 of class Test.java:
String s; // line 10
int l = s.length(); // line 11
System.out.println(“length is “+ l ); // line 12
When you compile, you get the following message:
Test.java:11: variable s might not have been initialized.
int l = s.length( ); // line 11
^
1 error
Explain what the problem is and how to fix it.
The Account class has two of the public methods shown as follows.
Public class Account {
// other code …
public void setName(String name){
// code omitted
} // set name of the account
public String getName(){
// code omitted;
} // returns the name of the account.
// other code… …
}
The client code is as follows:
public static void main(String[] args) {
// declaring 2 objects of Account class
Account account1, account2;
account1 = new Account("Jane Green", 500.0);
account2 = account1;
account2.setName(“Phil Grey”);
System.out.println("account1 owner: “ + account1.getName());
System.out.println("account2 owner: “ + account2.getName());
}
What is the output?
Part VI. Write Short Programs
Write a program that takes two words as input from the keyboard, representing a password and the same password again. (Often, web- sites ask users to type their password twice when they register to make sure there was no typo the first time around.) Your program should do the following:
If both passwords match, then output "You are now registered as a new user"
otherwise, output "Sorry, passwords don’t match"
(5 points)
Step-A. Write a class encapsulating the concept of a student, assuming a student has the following attributes:
a name
a student ID
a GPA (for instance, 3.8)
Please include
a constructor,
the setter and getter methods, and
a public method to print out the student information.
Step-B. Write a client class (so called controlling class) to test all the methods in the above class.