Week 6 slides (with answers for HW 1)

advertisement
Review
Review
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
java
DOS: javac and java
Project 1 (Section 01)
• Average: 93.14 (35 students
submitted)
Project 1
100
80
60
40
20
0
Project 1
0
10
20
30
40
Project 1 (Section 2)
• Average: 97 (only 28 students
submitted)
101
100
99
98
97
Series1
96
95
94
93
0
5
10
15
20
25
30
Lab section
Homework 1 (Section 01)
• Avg =84.3. Max = 99.
Section01
120
100
80
60
Series1
40
20
0
0
10
20
30
40
Homework 1 (Section 02)
• Avg = 88, Max = 100
Section 02
120
100
80
60
Series1
40
20
0
0
10
20
30
40
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
This is the only
d. java and link
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. .java
b. .class
c. .src
d. .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 sensitive
a. True
b. 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. True
b. False
Homework1
8. Which of the following is an invalid
identifier?
a. one
b. Two
c. _3Four
d. 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;
Homework1
10. 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 declared
c. Variable b has been declared
d. 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-alphanumeric)_____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 1
21.
double monthlyInterestRate;
if (monthlyInterestRate >= 0.08) &&
(monthlyInterestRate <= 0.5 ); {
System.out.println(“Interest rate is invalid”);
System.exit(1);
1.
2.
3.
}
() is needed or remove )( before and after &&
; should be take out
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 ;
2. { should be inserted here
}
else { System.out.println(“ Drop large
cup into position”);
}
else System.exit(-1);
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 semicolon
2. Missing post body update (increment or
decrement)
3. semicolon after 100) should be taken
out
Homework 1
25. int x,y;
x=-10;
y=-20;
if ( y>0 && x< y)
System.out.println(" The output is "+(x+y));
else
System.out.println(" The output is "+(x-y));
The output is 10
(Because -10-(-20) = -10+20 = 10)
Homework 1
26. int count=0, sum=0;
while (count < 10) {
sum += count;
count+=3;
}
System.out.println(" Sum ="+ sum);
count=0
sum =0
count = 3
sum = 0+3=3
count = 6
sum = 3+6=9
count = 9
sum = 9+9=18
Homework 1
27. int nextToPrint = 1;
int maximum = 10;
while(nextToPrint <= maximum){
if (nextToPrint % 2 != 0)
System.out.print(nextToPrint+" ");
nextToPrint = nextToPrint +1;
}
System.out.println();
13579
Homework 1
28. int
sum=0;
int i=0;
Sum = 30
while (i<5) {
int j=4;
while (i!=j) {
sum += j;
j=j-1;
}
i++;
}
System.out.println(" Sum ="+sum);
Homework 1
• sum=0 i=0
• j=4
sum = 0+4 = 4
j=3
sum = 4+3 = 7
j=2
sum = 7+2 = 9
j=1
sum = 9+1=10
Homework 1
• i=1
• j=4
sum = 10+4 = 14
j=3
sum = 14+3 = 17
j=2
sum = 17+2 = 19
Homework 1
• i=2
• j=4
sum = 19+4 = 23
j=3
sum = 23+3 = 26
Homework 1
• i=3
• j=4
sum = 26+4 = 30
Download