Professional Documents
Culture Documents
PROGRAMMING
FUNDAMENTALS
ASSIGNMENT # 01
QUESTION # 1:
a. Write JAVA statements that can produce Syntax Errors. Give three different examples and
write the names of the errors
AMAAN AHMAD SP22-BCT-004 CYBER SECURITY
PROGRAMMING FUNDAMENTALS 2ND SEMESTER
b. Write JAVA statements that can produce Logical Errors. Give three different examples and
briefly explain the reason (1-2 lines)
1. This will execute the result we want will not come due to the sign being
incorrect.
2. The syntax is correct, but the semicolon is not right after the statement.
c. Write JAVA statements that can produce Run Time Errors. Give three different examples and
briefly explain the reason (1-2 lines)
1.
This string is stored with some value, but it will produce a runtime error because the value of the
string is null.
AMAAN AHMAD SP22-BCT-004 CYBER SECURITY
PROGRAMMING FUNDAMENTALS 2ND SEMESTER
2.
This is also a true statement but will not execute because of integer can not be divided by a zero.
d. The following program has syntax errors. Write the type of error and its correction (in
tabular form). After you have corrected the syntax errors, show the output of this program.
Errors are generated in proper order from the start of the program to the
end of the program:
1. args should be written instead of arg.
2. PRIME is not declared but is used which will produce an error.
3. The data type of variable x is not defined which is double.
4. ONE is also not defined.
5. The way sum + count = sum; is written is incorrect. It should be sum = sum + count;
6. COUNT is used instead of count.
7. The output statement must be written in a single line.
8. Prime is used instead of PRIME.
AMAAN AHMAD SP22-BCT-004 CYBER SECURITY
PROGRAMMING FUNDAMENTALS 2ND SEMESTER
QUESTION # 2:
You are required to identify the following (Show your answer in tabular form)
Comments = { /*This program will calculate product of three numbers */, // first number, // second
number, // third number, //product of numbers}
Identifier = { class, num1, static, args, Strings, public, num2, num3, Test, main }
Standard Input Stream Object { No Input was given in the above program all the values were
initialized at the time of declaration}
QUESTION # 3:
5. Copy the value of an int variable firstNum into an int variable tempNum.
6. Swap the contents of the int variables x and y.(Declare additional variables, if
necessary.)
7. Declare a char variable grade and set the value of the grade to 'A'.
9. Copy the value of a double variable z to the nearest integer into an int variable x.
B) Suppose a, b and c are int variables and a = 5, b = 6, d = 2. What value is assigned to each variable
after each statement executes? If a variable is undefined at a particular statement, report UND
(undefined)
Statements a b c d
a = (b++) + 3 * ++d; 15 7 UND 3
c = 2 * d + (++b) + a; 5 7 16 2
AMAAN AHMAD SP22-BCT-004 CYBER SECURITY
PROGRAMMING FUNDAMENTALS 2ND SEMESTER
C) Suppose a, b, and sum are int variables and c is a double variable. What value is assigned to each
variable after each statement executes? Suppose a = 3, b = 5, and c = 14.1
Statements a b c sum
sum = a + b + (int) c; 3 5 14.1 22
c /= a; 3 5 4.7
b += (int) c - a; 3 16 14.1
a *= 2 * b + (int) c; 72 5 14.1
Question – 4:
Research several car-pooling websites. Create an application that calculates your daily driving cost,
so that you can estimate how much money could be saved by carpooling, which also has other
advantages such as reducing carbon emissions and reducing traffic congestion. The application
should input the following information and display the user’s cost per day of driving to work:
a) Total miles are driven per day.
b) Cost per gallon of gasoline.
c) Average miles per gallon.
d) Parking fees per day.
e) Tolls per day.
AMAAN AHMAD SP22-BCT-004 CYBER SECURITY
PROGRAMMING FUNDAMENTALS 2ND SEMESTER
QUESTION # 5: Write an application that inputs one number consisting of five digits from the user
and shows the number in reverse order. Note the reverse order must also be a number. E.g. 93324