You are on page 1of 7

AMAAN AHMAD SP22-BCT-004 CYBER SECURITY

PROGRAMMING FUNDAMENTALS 2ND SEMESTER

PROGRAMMING
FUNDAMENTALS
ASSIGNMENT # 01

NAME: AMAAN AHMAD


REG. # SP22-BCT-004
COURSE: CYBER SECURITY
SEMESTER: 2ND
SUBJECT: PROGRAMMING FUNDAMENTALS
ASSIGNMENT SUBMITTED TO MAM HAFSA MOHSIN

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

1. Error occurred as the semicolon (;) was not included.

2. Not identifying the data type.

3. Not including the ending


bracket

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.

3. the syntax is correct but the logic of including


the comment is not right and will generate a logical error.

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.

public class Test{


public static void main(String[] arg){
count = 1;
sum = count + PRIME;
x := 25.67;
newNum = count * ONE + 2;
sum + count = sum;
x = x + sum * COUNT;
System.out.println(" count = " + count + ", sum = "
+ sum + ", PRIME = " + Prime);
}
}

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:

Consider following JAVA Code


/*This program will calculate the product of three numbers */
public class Product{
public static void main(String[] args){
int num1 = 10; // first number
int num2 = 20; // second number
int num3 = 1;// third number
int result; //product of numbers
result = num1 * num2 * num3;
System.out.println("Product of numbers: "+result);
}
}

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}

Special symbols = { //, /*, /n }

Reserve words = { main, class, int}

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}

Standard Output Stream Object = { "Product of numbers: "}

QUESTION # 3:

A) Write Java statements that accomplish the following.


1. Declare int variables x and y.

2. Initialize an int variable x to 10 and a char variable ch to 'B'.


AMAAN AHMAD SP22-BCT-004 CYBER SECURITY
PROGRAMMING FUNDAMENTALS 2ND SEMESTER

3. Update the value of an int variable x by adding 5 to it.

4. Declare and initialize a double variable payRate to 12.50.

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'.

8. Declare int variables to store four integers.

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

b = 2 * (++c) - (a++); Not running Not running UND Not running


Due to no Due to no Due to no
value being value is value is
initialized initialized initialized
to c to c to c
d = d++ + d + b++ + b; 5 7 18

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

The reverse order number: 4 2 3 3 9

You might also like