ReviewReview
Review
Concepts
• Comments: definition, example, different types of comments
• Class: definition, example• Object: definition, example• Data type:
– Byte, short, int, long, float, double– Char, boolean– String
Concepts
• Assignment statement• Boolean expressions• Identifiers, constants• Commands:
– If– Switch– Do/While, While, and For
Concepts
• File extension: *.java, *.class• Compile: Textpad: Compile Java and Run
javaDOS: javac and java
Project 1 (Section 01)
• Average: 93.14 (35 students submitted)
Project 1
020406080
100
0 10 20 30 40
Project 1
Project 1 (Section 2)
• Average: 97 (only 28 students submitted)
93
94
95
96
97
98
99
100
101
0 5 10 15 20 25 30
Series1
Lab section
Homework 1 (Section 01)
• Avg =84.3. Max = 99.
Section01
0
20
40
60
80
100
120
0 10 20 30 40
Series1
Homework 1 (Section 02)
• Avg = 88, Max = 100
Section 02
0
20
40
60
80
100
120
0 10 20 30 40
Series1
Rules for Midterm exam - Student ID is required- Don’t:
- Leave the exam room after the exam is distributed
- Use cell phone or computers (PC)- Talk with each other during the exam
- Limit of questions you can ask teacher during exam time. If you ask more than two questions, you will have some points off taken from your exam.
Answers to Homework 1
1. In general, commands to compile and execute a java program are
a. compile and run b. compile and link c. javac and java d. java and link
This is the only answer accepted if this question is asked in another
exam
Homework 1 - Answer
2. All Java program must have this method to run
a. hello()b. start()c. main()d. method()
Homework 1
• 3. Which of the following is not mandatory in variable declaration
• a. a semicolon • b. an assignment • c. a variable name• d. a data type
Homework1
4. You store java source code files with following extension?
a. .javab. .classc. .srcd. .javadoc
Homework1
5. A single line comment in Java starts with
a. ; b. # c. , d. // e. none of the above
Homework1
6. Java language is case sensitivea. Trueb. False
Homework1
7. As long as a computer has a Java Virtual Machine, the same program written in the Java programming language can run on any computer
a. Trueb. False
Homework1
8. Which of the following is an invalid identifier?
a. oneb. Twoc. _3Fourd. 4Five
Homework1
9. Which of the following is an invalid variable declaration?
a. String inputStr;b. int i=0;c. boolean flag = true;d. double, int, j=0;
Homework110. Assuming the following declarations are
executed in sequence, why are the second and the third declarations invalid?int a,b;double a;double b;
a. Both a and b are invalid identifiers.b. Variable a has been declaredc. Variable b has been declaredd. Both variables a and b have been
declared in the first declaration
Homework1
11. The __new_____operator is used to create a new object
Homework 1
12. Identifiers may not start with a ___number (digit)________ or contain__special/(non-alpha-numeric)_____characters, other than the underscore or the dollar sign
Homework 1
13. char data type represents individual _character______
Homework 1
14. Text data can be stored in _String__objects
Homework 1
15. Constants __can not be (never be, are not)__changed during program execution
Homework 1
16. Boolean expressions are built by use of _relational___operators and _boolean__________operators
Homework 1
17. Every variable that we use in a Java program must be _declared/defined__________
Homework 1
18. A Java program is composed of one or more ___classes__
Homework 1
20. int a== b;== can’t be used in variable
declaration
Homework 121. double monthlyInterestRate;
if (monthlyInterestRate >= 0.08) && (monthlyInterestRate <= 0.5 ); {
System.out.println(“Interest rate is invalid”);System.exit(1);
}1. () is needed or remove )( before and
after &&2. ; should be take out3. No value has been initialized for
monthlyInterestRate
Homework 1
22. char userChoice=’l’;if (userChoice ==’s’) {
System.out.print(“ Drop small cup into position”); (1) Missing ;}else { System.out.println(“ Drop large cup into position”);
}else System.exit(-1);
2. { should be inserted here
Else should be taken out
Homework 1
23. String choice=”5”;switch(choice) {case 4: System.out.println(“ Right branch”); break;default: System.out.println(“ Default branch”); break;}
1. Switch doesn’t support String data type. It must be char or int(bytes,short,int)
Homework 1
24. int sum=0;
for (int i=0, i< 100);{sum+= i;
}1; int i=0, this comma should be semicolon2. Missing post body update (increment or
decrement)3. semicolon after 100) should be taken out
Homework 125. int x,y;
x=-10;y=-20;if ( y>0 && x< y)System.out.println(" The output is "+(x+y));elseSystem.out.println(" The output is "+(x-y));
The output is 10(Because -10-(-20) = -10+20 = 10)
Homework 126. int count=0, sum=0;
while (count < 10) {sum += count;count+=3;
}System.out.println(" Sum ="+ sum);count=0 sum =0count = 3 sum = 0+3=3count = 6 sum = 3+6=9count = 9 sum = 9+9=18
Homework 127. int nextToPrint = 1;
int maximum = 10; while(nextToPrint <= maximum){
if (nextToPrint % 2 != 0) System.out.print(nextToPrint+" ");
nextToPrint = nextToPrint +1; }
System.out.println();
1 3 5 7 9
Homework 128. int sum=0;
int i=0;while (i<5) {
int j=4;while (i!=j) {
sum += j;j=j-1;
}i++;
}System.out.println(" Sum ="+sum);
Sum = 30
Homework 1
• sum=0 i=0• j=4
sum = 0+4 = 4j=3
sum = 4+3 = 7j=2
sum = 7+2 = 9j=1
sum = 9+1=10
Homework 1
• i=1• j=4
sum = 10+4 = 14j=3
sum = 14+3 = 17j=2
sum = 17+2 = 19
Homework 1
• i=2• j=4
sum = 19+4 = 23j=3
sum = 23+3 = 26
Homework 1
• i=3• j=4
sum = 26+4 = 30