ICS 111 Review Session - October 5

Today we will be reviewing for the midterm on Thursday (Oct. 7).  As of this class, you have 48 hours to prepare for the exam.  Ask yourself how you would do on the exam if you were to take it right now, and make yourself a plan to achieve the grade that you would like to earn.  Please keep in mind that the concepts you are learning are like Mathematics - we are teaching you a skill set to logically think through problems and come up with answers to them.  Just like a Math class, you will probably not learn as much by glancing over your book as you would by practicing problems and running through examples.  A good idea to help prepare for the exam is  by making short test programs on UH UNIX, and then compiling and running them like you do in the lab.  Practice makes perfect...

Past Quiz Questions

Questions from Past Midterms

Extra Practice Problems


Review of Past Quizzes: (with answers)

Quiz 1:

Java is an example of a(n)
a) machine language
b) assembly language
c) high-level language
d) fourth generation language
e) all of the above
Answer: c. Explanation: While Java was created during the fourth generation, it is clearly a high-level language. Machine language is the executable language of a machine, with programs written in 1s and 0s only. Assembly language uses mnemonics. Fourth generation languages are tools wrapped inside of programs so that the user has the flexibility to write some code to executed from within the program.

The main method for a Java program is defined by
a) public static void main( )
b) public static void main(String[ ] args);
c) public static void main(String[ ] args)
d) private static void main(String[ ] args)
e) the main method could be defined as in a, c or d but not b
Answer: c. Explanation: In a, the missing parameter is not allowed. The parameters are defined later in the text, but in affect, they allow the user to run the program and include some initial arguments if the program calls for it. In b, the semicolon at the end of the statement is not allowed. In d, “private” instead of “public” would make the program unexecutable by anyone and thus makes the definition meaningless.

The line of Java code “// System.out.println("Hello");” will
a) do nothing
b) cause "Hello" to be output
c) cause a syntax error
d) cause "(Hello)" to be output
e) there is no way to know without executing this line of code
Answer: a. Explanation: The characters “//” denote the beginning of a comment. The comment is not compiled and so, nothing would happen when this code is executed.

Which character below is not allowed in an identifier?
a) $
b) _
c) 0 (zero)
d) q
e) ^
Answer: e. Explanation: Java identifiers can consist of any letter, digit, $ or _ as long as the identifier starts with a letter or _. ^ is not a legal character.

Which of the following would be a legal Java identifier?
a) i
b) class
c) ilikeclass!
d) idon'tlikeclass
e) i-like-class
Answer: a. Explanation: Java identifiers cannot have the characters “!”, “'” or “-” in them making answer c, d and e wrong. The word “class” is a reserved word in Java and cannot be used as an identifier. The identifier “i” is perfectly legal although not necessarily a good identifier since it is not descriptive of its use.

A unique aspect of Java that allows code compiled on one machine to be executed on a machine of a different hardware platform is Java’s
a) bytecodes
b) syntax
c) use of objects
d) use of exception handling
e) all of the above
Answer: a. Explanation: The translation process for a Java program is to first compile it into bytecodes, which are architecturally neutral (that is, they can be used no matter what the architectural platform is). To execute the program, the bytecodes must be further compiled by a Java compiler or interpreted by a Java Virtual Machine.

An error in a program that results in the program outputting $100 instead of the correct answer, $250 is
a) a programmer error
b) a syntax error
c) a run-time error
d) a logical error
e) a snafu
Answer: d. Explanation: While this is an error (answer a), programmers classify the type of error in order to more easily solve the problem. Syntax errors are caught by the compiler and the program cannot run without fixing all syntax errors. Run-time errors arise during program execution and cause the program to stop running abnormally. Logical errors are errors whereby the program can run to completion, but gives the wrong answer. If the result should have been $250, then the logic of the program is wrong since it output $100. A snafu is a term expressing a messed up situation in combat and should not be used by respectable programmers!

Which of the following is true regarding Java syntax and semantics?
a) a Java compiler can determine if you have followed proper syntax but not proper semantics
b) a Java compiler can determine if you have followed proper semantics but not proper syntax
c) a Java compiler can determine if you have followed both proper syntax and semantics
d) a Java compiler cannot determine if you have followed either proper syntax or semantics
e) a Java compiler can determine if you have followed proper syntax and can determine if you have followed proper semantics if you follow the Java naming convention rules
Answer: a. Explanation: Compilers in any language have the ability to detect syntax errors because improper use of the syntax leads to situations where the compilers cannot translate the code properly. However, compilers are unable to follow the semantics of a program because this requires a degree of understanding what the program is intended to do and computers have no sense of understanding (at least to this point).

Following Java naming convention, which of the following would be the best name for a class about store customers?
a) StoreCustomer
b) Store Customer
c) storeCustomer
d) STORE_CUSTOMER
e) Store-Customer
Answer: a. Explanation: The Java naming convention states that classes should all start with an upper case letter and that multiple-word names should start each new name with an upper case letter while the remaining characters are lower case. Words should either be connected together without spaces, or connected with the “_” character. Answers b and e are not legal names, and using Java naming convention, c would qualify as a variable name and d would qualify as a constant.

Mistyping “println” as “printn” will result in
a) a compiler error
b) a run-time error
c) a logical error
d) no error at all
e) converting the statement into a comment
Answer: a. Explanation: If the Java compiler cannot make sense of a command, the compiler cannot convert it and responds with a syntax error. While “println” is recognized as a command, “printn” is not, and so the compiler provides a syntax error.

System.out.print is used in a program to denote that a comment follows.
Answer: False. Explanation: Comments follow // marks or are embedded between /* and */. System.out.print is an instruction used to output a message to the screen (Java console window).

Java is a case-sensitive language meaning that Current, current and CURRENT will all reference the same identifier.
Answer: False. Explanation: Java is case sensitive which means that Current, current and CURRENT will all be recognized as different identifiers. This causes problems with careless programmers who do not spell an identifier consistently in terms of upper and lower case characters.


Quiz 2:

Use the following class definition to answer questions 1-2.

public class Questions1And2{
  public static void main(String[ ] args){
    System.out.print("Here");
    System.out.println("There " + "Everywhere");
    System.out.println("But not" + "in Texas");
  }
}

1) The program will print the word "Here" and then print
a) "There Everywhere" on the line after "Here"
b) "There" on the line after "Here" and "Everywhere" on the line after "There"
c) "There Everywhere" on the same line as "Here"
d) "ThereEverywhere" on the same line as "Here"
e) "ThereEverywhere" on the line after "Here"
Answer: c. Explanation: System.out.print will output the word "Here" but will leave the cursor at
that point rather than starting a new line. The next statement will output "There Everywhere"
immediately after the word "Here". Since there is a blank space within the quote marks for "There",
there is a blank space inserted between "There" and "Everywhere".

2) The final println command will output
a) "But not in Texas"
b) "But notin Texas"
c) "But not" on one line and "in Texas" on the next line
d) "But not+in Texas"
e) "But not + in Texas"
Answer: b. Explanation: The .+. performs String concatenation, so that "But not" and "in Texas" are
concatenated together. Notice that there is no blank space after "not" or before "in" so that when they
are concatenated, they are placed together without a blank space.

3) Which of the following would return the last character of the String x?
a) x.charAt(0);
b) x.charAt(last);
c) x.charAt(length(x));
d) x.charAt(x.length( )-1);
e) x.charAt(x.length( ));
Answer: d. Explanation: Since last is not defined, b is syntactically invalid. The 0th character is
the first in the String, so a is true only if the String has a single character. The answer in c is
syntactically invalid as length can only be called by passing the message to x. Finally, d and e are
syntactically valid, but since length returns the size of the String, and since the first character
starts at the 0th position, the last character is at x.length()-1, so e would result in a run-time
error.

4) What will be the result of the following assignment statement? Assume b = 5 and c = 10.
int a = b * (-c + 2) / 2;
a) 30
b) .30
c) 20
d) .20
e) .6
Answer: d. Explanation: The unary minus is applied first giving .c + 2 = -8. Next, the * is
performed giving 5 * -8 = -40, and finally the / is performed giving .40 / 2 = -20.


5) Which of the following is true regarding the mod operator, %?
a) It can only be performed on int values and its result is a double
b) It can only be performed on int values and its result is an int
c) It can only be performed on float or double values and its result is an int
d) It can only be performed on float or double values and its result is a double
e) It can be performed on any numeric values, but its result is always an int
Answer: b. Explanation: Mod, or modulo, returns the remainder that results from an integer division.
The remainder is also an integer.

6) Assume that x, y and z are all ints equal to 50, 20 and 6 respectively. What is the result of x
/ y / z?
a) 0
b) 12
c) 16
d) A syntax error as this is syntactically invalid
e) A run-time error because this is a division by 0
Answer: a. Explanation: This division is performed left to right, so first 50 / 20 is performed.
Since 50 and 20 are ints, this results in 2. Next, 2 / 6 is performed which is 0. Notice that if the
division were performed right to left, the evaluation would instead be 50 / (20 / 6) = 50 / 3 = 16.


*********************************************************************************

1) Consider the following statement:
System.out.println("1 big bad wolf\t8 the 3 little pigs\n4 dinner\r2night");
This statement will output ___ lines of text
a) 1
b) 2
c) 3
d) 4
e) 5
Answer: b. Explanation: The \t escape sequence inserts a tab, but leaves the cursor on the same line.
The \n escape sequence causes a new line to be produced so that .4 dinner. is output on the next line.
The escape sequence \r causes the carriage to return (that is, the cursor to be moved back to the left
margin) but because it does not start a new line, .2night. is output over .4 dinn. resulting in a second
line that looks like .2nighter..

2) If you want to output the text "hi there", including the quote marks, which of the following
could do that?
a) System.out.println("hi there");
b) System.out.println(""hi there"");
c) System.out.println("\"hi there");
d) System.out.println("\"hi there\"");
e) none, it is not possible to output a quote mark because it is used to mark the beginning and
ending of the String to be output.
Answer: d. Explanation: \" is an escape sequence used to place a quote mark in a String, so it is
used here to output the quote marks with the rest of the String.

3) What value will z have if we execute the following assignment statement?
float z = 5 / 10;
a) z will equal 0.0
b) z will equal 0.5
c) z will equal 5.0
d) z will equal 0.05
e) none of the above, a run-time error arises because z is a float and 5 / 10 is an int
Answer: a. Explanation: 5 and 10 are both int values, so 5 / 10 is an integer division. The result
is 0. Even though z is a float and can store the real answer, 0.5, it only gets 0 because of the
integer division. In order to get 0.5, we would have to first cast 5 or 10 as a float.

4) A cast is required in which of the following situations?
a) using charAt to take an element of a String and store it in a char
b) storing an int in a float
c) storing a float in a double
d) storing a float in an int
e) all of the above require casts
Answer: d. Explanation: For a, charAt returns a char, so there is no problem. In b and c, the
situations are widening operations taking a narrower type and storing the value in a wider type. Only
in d is there a situation where a wider type is being stored in a narrower type, so a cast is required.

5) If x is an int and y is a float, all of the following are legal except which assignment
statement?
4) y = x;
5) x = y;
6) y = (float) x;
7) x = (int) y;
8) all of the above are legal
Answer: b. Explanation: Since x is an int, it cannot except a float unless the float is cast as an
int. There is no explicit cast in the assignment statement in b. In a, a cast is not necessary because
a float (y) can accept an int value (x), and in c and d, explicit casts are present making them legal.

6) Suppose that String name = "Frank Zappa". What will the instruction name.toUpperCase(
).replace('A', 'I'); return?
a) "FRANK ZAPPA "
b) "FRINK ZIPPI"
c) "Frink Zippi"
d) "Frank Zappa"
e) "FrInk ZIppI"
Answer: b. Explanation: The toUpperCase method returns the String as all upper case characters, or
"FRANK ZAPPA". The replace method will replace each instance of 'A' with 'I'.
 

(back to top)


Sample Midterm Questions from past semesters (solutions at bottom):

Sample Midterm Exam 1
For Review ICS 111, Fall 2004
This exam was given on Feb 19th 2004

Note:  Use a spare sheet of paper to answer these- the original test had lots of space, which was removed to shorten the handout size

Programming (30 points)

 1.  Write a program that will read a number from the user.  The number will be tested and:

 

2.  How will JAVA evaluate the following conditions? For full credit explain step by step. (10 points each, 20 points total) 

int a = 5;

int b = 8;

int c = 14;

double dx = 3.4;

double dy = 5.9;

double dz = 6.2;

if((c%a == 4)||(a-b*c >0)&&(dy > a)){

        System.out.println("true");

}

else{

        System.out.println("false");

}

 if((c%a == 4)||(a-b*c >0)&&(dy > a))

  

int a = 5;

int b = 8;

int c = 14;

double dx = 3.4;

double dy = 5.9;

double dz = 6.2;

if((c-a/3 < 4)||(dx+dy*2<100)&&(a-b/c <0)){

        System.out.println("true");

}

else{

        System.out.println("false");

}

 if((c-a/3 < 4)||(dx+dy*2<100)&&(a-b/c <0))

 

Debugging (30 points total, 3 points each)

if any of the following statements will cause a compiler error please write in your own words the compiler error instead of the program output then skip the problem step and continue on... 

        String s1="JAVALAVAGUAVA";

        String s2="     java is  very   cool     "; //5,1,2,3,5 spaces

        String s3="counting, 12345";

        int iNum=25;

        double d1 = 4.8;

        double d2 = 5.1;

                                                                        Println statements here:

System.out.println(s1.substring(3,4));

 

s1=s1.substring(7);
System.out.println(s1.toUpperCase( ));

 

System.out.println(s2.trim( ));

 

System.out.println(s2.length( ));

 

s1 = s3.substring(11);

        try{

            iNum = Integer.parseInt(s1);

        }

        catch(NumberFormatException nfe){

            iNum = iNum+2;

        }

if(iNum%3 == 0){

           System.out.println("It is: "+ iNum );

        }

        else{

           System.out.println("It is still: "+ iNum );

        }

 

System.out.println(s2 + s3);

 

iNum = (int)(d1+d2);

System.out.println("result integer = " + iNum );

 

d2 = (int)(d1+d2);

System.out.println("result of doubles = " + d2 );

 

d2 = d1 + (s2.length( )/2) ;

System.out.println("result and length = " + d2 );

 

d1 = d1 +iNum;

System.out.println("last one " + d1 );

 

 

Solutions to the above questions can be found in the following two Java programs:  Exam111.java    Tester.java

(back to top)


Some Extra Problems to Review:

* some extra exercises for while and for loops (practice this after class)
For this answer questions such as:

What does this loop print?
It this an infinite loop?
How can you make this an infinite loop?

Here are a couple of examples:
=======================================================================
If x is an int where x = 0, what will x be after the following loop terminates?
while (x < 100)
x *= 2;
a) 2
b) 64
c) 100
d) 128
e) None of the above, this is an infinite loop
Answer: e. Explanation: Since x starts at 0, x *= 2; results in 0 * 2, or x = 0. Since x remains 0, (x < 100) remains true, and the loop repeats. x continues to be 0 and so the condition never changes to false, and thus the loop never terminates.
=======================================================================
Given the following code, where x = 0, what is the resulting value of x after the for-loop terminates?
for(int i=0;i<5;i++)
x += i;
a) 0
b) 4
c) 5
d) 10
e) 15
Answer: d. Explanation: Each pass through the for-loop results with the current value of the loop index, i, being added to x. The first time through the loop, i = 0 so x = x + 0 = 0. The second time through the loop i = 1 so x = x + 1 = 1. The third time through the loop i = 2 so x = x + 2 = 3. The fourth time through the loop i = 3 so x = x + 3 = 6. The fifth and final time through the loop i = 4 so x = x + 4 = 10.


Some True / False:

=======================================================================
In Java, the symbol .=. and the symbol .==. are used synonymously (interchangeably).

Answer: False. Explanation: .=. is used for assignment statements while .==. is used to test equality.
=======================================================================
The statement if(a >= b) a++; else b--; will do the same thing as the statement if (a < b) b--; else a++;.

Answer: True. Explanation: We can reverse the if clause and else clause if we reverse the condition. The opposite condition of (a >= b) is (a <
b) so this works out logically. Note that if we used the condition (a <=
b) then the resulting statement would not do the same thing as the original if a==b.
=======================================================================
An if statement may or may not have an else clause, but an else clause must be part of an if statement.

Answer: True. Explanation: Java allows for either if or if-else statements. But else is only used as part of an if statement.
=======================================================================
In order to compare int, float and double variables, you can use <, >, ==, !=, <=, >=, but to compare char and String variables, you must use compareTo( ), equals( ) and equalsIgnoreCase( ).

Answer: False. You can also directly compare char variables using <, >, ==, !=, <=, >=, but you must use compareTo( ), equals( ) and equalsIgnoreCase( ) for any String comparisons.
=======================================================================
If String a = "ABCD" and String b = "abcd" then a.equals(b); returns false buta.equalsIgnoreCase(b); returns true

This is true
=======================================================================

(back to top)