Professional Documents
Culture Documents
PF Assignment 1
PF Assignment 1
PF Assignment 1
JAVA statement:
( ( ( 3 + 4 * x ) / 5 ) – ( 10 * ( y -5 ) * ( a + b + c ) / x ) + 9 * ( ( 4 / x ) + ( ( 9 + x ) / y ) ) )
b) 5.5* ((𝑟+2.5)2.5+𝑡)?
JAVA Statements:
5.5 * ( Math.pow ( ( r + 2.5 ) , 2.5 + t )
Precedence: (4) (3) (1) (2)
c) 4 3(𝑡+34) − 9(𝑎+𝑏𝑐) +3+𝑑(2+𝑎)𝑎+𝑏𝑑?
Java Statement:
( 4 / ( 3 * ( t + 34 ) ) ) – ( 9 * ( a + b * c ) ) + ( ( 3 + ( d * ( 2 + a ) ) ) / ( a + b * d ) )
(QUESTION # 2)
Determine the hierarchy of operations and evaluate the following expression:
a) 9.5∗4.5−2.5∗345.5−3.5?
9.5 * 4.5 - 2.5 * 3 / 45.5 -3.5 ( left most multiplication)
42.7 - 2.5 * 3 / 45.5 – 3.5 ( multiplication before subtraction)
42.7 - 7.5 / 45.5 - 3.5 (division before subtraction)
42.7 - 0.16 – 3.5 (left most subtraction)
42.5 – 3.5 (last operation of minus)
39.04 (Output)
g = big / 2 + big * 4 / big - big + abc / 3 (Put values of abc and big)
g = 2 / 2 + 2 * 4 / 2 – 2 + 2.5 / 3 (Left most Division)
g = 3 + 0.83333 (Addition)
g = 3.83333 (OUTPUT)
be an int)?
on = ink * act / 2 + 3 / 2 * act + 2 + tig (Put values of ink, act and tig)
on = 9.5 (OUTPUT)
s=2–3+ 2 (Substraction)
s = -1 + 2 (Addition)
s=1 (OUTPUT)
e) s = 1 / 3 * a / 4 - 6 / 2 + 2 / 3 * 6 / g ; (a = 4, g = 3, assume s to be an int?
s = 0.333 - 3 + 4 / 3 ( Division)
s = -1.3334 (OUTPUT)
(QUESTION # 3)
Given that y = ax3 + 7, which of the following are correct Java statements for this
equation?
a) y = a * x * x * x + 7;
This Java statement is correct, as the given expression is ax^3 +7 by evaluating the
following statement would give the correct answer cause proper precedence order is
follow in this.
b) y = a * x * x * ( x + 7 );
This Java statement is incorrect as the precedence is not follow in a right way cause
by using the bracket and adding x to 7 add different meaning to the expression as x is
not a power but is adding in 7 so the answer will be different not the one we want.
c) y = ( a * x ) * x * ( x + 7 );
This Java statement is incorrect because of the same reason that precedence order is
followed with right method by using bracket in last and adding x to 7 has change tge
output of the expression so the output will be different and this will give wrong
answer as it is not evaluated with right precedence order.
d) y = ( a * x ) * x * x + 7;
This Java statement is correct, as the given expression is ax^3 +7 by evaluating the
following statement would give the correct answer cause proper precedence order is
follow in this.
e) y = a * ( x * x * x ) + 7;
This Java statement is correct, as the given expression is ax^3 +7 by evaluating the
following statement would give the correct answer cause proper precedence order is
follow in this.
f) y = a * x * ( x * x + 7 );
This Java statement is incorrect due to the incorrect use of precedence order which
will change the output and will not give the correct output so proper precedence
order should be followed with the multiplication first then addition would give the
correct answer.
(Question # 4)
State the order of evaluation of the operators in each of the following Java
statements, and show the value of x after each statement is performed:
a) x = 7 + 3 * 6 / 2 - 1;
1) * , 2) / , 3) + , 4) -
x=7+3*6/2–1 (Multiplication)
x = 7 + 18 / 2 – 1 (Division)
x=7+9–1 (Addition)
x = 16 – 1 (Substraction)
x = 15 (OUTPUT)
b) x = 2 % 2 + 2 * 2 - 2 / 2;
1) % , 2) * , 3) / , 4) + , 5) –
x=2%2+2*2-2/2 (Remainder)
x=0+2*2-2/2 (Multiplication)
x=0+4-2/2 (Division)
x=0+4–1 (Addition)
x = 4 -1 (Substraction)
x=3 (OUTPUT)
c) x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) );
x=(3*9*(3+(9*3/(3)))) (Bracket 1)
x = ( 3 * 9 * ( 3 + ( 27 / 3 ) ) ) (Bracket 2, Division)
x = ( 27 * 12 ) (Multiplication)
x = 324 (OUTPUT)
(Question # 5)
OUTPUT:
*
***
*****
****
**
(Question # 6)
OUTPUT:
***************
(Question # 7)
What does the following code print?
System.out.print( "*" );
System.out.println( "***" );
System.out.println( "*****" );
System.out.print( "****" );
System.out.println( "**" );
OUTPUT:
****
*****
******
(Question # 8)
double k = 1 + 50.0;
double j – k + 1;
Errors:
Correction:
Output:
System.out.println((9 / 5) * 35 + 32);
Errors: we cant name the class name ShowLogicErrors as it gives and the closing
bracket is missing in the end.
Correction:
public class Error
System.out.println((9 / 5) * 35 + 32);
Output:
System.out.println(1 / 0);
}}
Correction:
System.out.println(1 / 0);
}}
Output:
No output
d) System.out.println("Problem Driven );
Error: Class is missing and or main method in it and also quotation from right side
is missing.
Correction:
public class Code{
System.out.println(“Problem Driven”);
}}
Output:
Problem Driven
Correction:
}}
double i = 50.0;
double k = i + 50.0;
double j = k + 1;
Error: static keyword is missing and string s is small and no class in the beginning.
Correction:
double i = 50.0;
double k = i + 50.0;
double j = k + 1;
Output:
Error: No class at start, no main method and also we use %n for escape character in
printf statement.
Correction:
Output:
int x = 10 ;
if ( x >= 2 )
Error: No class or main method and for escape character in printf use %n instead
of \n. The closing bracket is missing in end.
Correction:
Class Check{
int x = 10 ;
if ( x >= 2 )
Output:
10
int x = 10, y = 15 ;
if ( x % 2 = y % 3 )
Error: No class main method and in line 3 should == operator to check euality.
Correction:
Class Check{
int x = 10, y = 15 ;
if ( x % 2 == y % 3 )
Output:
Carpathians
(Question # 9)
What is the result of 25/4? How would you rewrite the expression if you wished the
result to be a floating-point number?
ANSWER: The result of 25/4 is 6 as it is a int division, if the value was 25.0 or 6.0 any
of them in floating point then the output will be different it would still be 6 but with the
decimal part so it will become 6.24 if it was floating point, since there is no value such
as floating point so we will ignore the fraction part and answer will be 6.
If I wished that the answer of this calculation to be in floating point then we have three
method to use and get our output in floating point.
(QUESTION # 10)
Show the result of the following remainders.
56 % 6
OUTPUT: 2
Remainder operator gives the values that remains after the division and cannot be
evaluated more. In 56%2 the output is 2 at the end that is remaining and cannot be
evaluated more.
78 % -4
OUTPUT: 2
Remainder operator gives the values that remains after the division and cannot be
evaluated more. In 78% -4 the output is 2 at the end that is remaining and cannot be
evaluated more and since the negative sign is with the divider so negative sign doesn’t
come with the remainder value.
-34 % 5
OUTPUT: -4
Remainder operator gives the values that remains after the division and cannot be
evaluated more. In -34 % 5 the output is -4 at the end that is remaining and cannot be
evaluated more and since the negative sign is with dividend so negative sign will also
come with the remainder output.
-34 % -5
OUTPUT: -4
Remainder operator gives the values that remains after the division and cannot be
evaluated more. In -34 % -5 the output is -4 at the end that is remaining and cannot be
evaluated more. The negative sign is along the output even when both values have
negative sign should be cancel out and output be positive, this will work in divison but
in remainder operation the sign with the dividend and output is same.
5%1
OUTPUT: 0
Remainder operator gives the values that remains after the division and cannot be
evaluated more. In 5 % 1 the output is 0 at the end that is remaining and cannot be
evaluated more.
1%5
OUTPUT: 1
Remainder operator gives the values that remains after the division and cannot be
evaluated more. In 1% 5 the output is 1 at the end that is remaining and cannot be
evaluated more.
(Question # 11)
Show the result of the following code:
System.out.println(2 * (5 / 2 + 5 / 2));
System.out.println(2 * 5 / 2 + 2 * 5 / 2);
System.out.println(2 * (5 / 2));
System.out.println(2 * 5 / 2);
OUTPUT:
8
10
4
5
(Question # 12)
Are the following statements correct? If so, show the output.
System.out.println("25 / 4 is " + 25 / 4);
System.out.println("25 / 4.0 is " + 25 / 4.0);
System.out.println("3 * 2 / 4 is " + 3 * 2 / 4);
System.out.println("3.0 * 2 / 4 is " + 3.0 * 2 / 4);
OUTPUT:
Yes, java statements are written without errors and their output is as follows:\
25 / 4.0 is 6.25 (The statement first display the text 25 / 4.0 is and then after
catenation symbol is used for non-string value into string and
display it in output, so the 25/4.0 is divided and the result is then
converted into string and is displayed with the rest of the
statement as a string.)
3 * 2 / 4 is 1 (The statement first display the text 3*2/4 is and then after
catenation symbol is used for non-string value into string and
display it in output, so the 3*2/4 is divided by using precedence
order and the result 1 is then converted into string and is
displayed with the rest of the statement as a string.)
3.0 * 2 / 4 is 1.5 (The statement first display the text 3.0 * 2 /4 is and then after
catenation symbol is used for non-string value into string and
display it in output, so the 3.0 * 2 / 4 is divided and the result is
1.5 which is float value it cannot display should be converted into
string, by using + the value 1.5 is then converted into string and is
displayed with the rest of the statement as a string.)
(Question # 13)
Part(b): Output
7
6
7
7
In this code we have assign a value 6 and then assign b value of a first which means b=6
and then assigning and incrementing a by 1 so a is now.
Part(c): Output
a=0 b=-6
Part(d): Output
There is a error in this code as we are storing float value in c when its data type is in int
and have to change it and also the values a and b are in int not float.
Correction:
int a = 5, b = 2 ;
int c ; c = a % b ;
System.out. printf ( "%d", c ) ;
Output:
1
Part(e): Output
nn
nn
nn/n/n nn/n
Part(f): Output
Enter values of p and q p = 8 q= 12
Part(g): Output
Dosa
(Question # 14)
Are the following statements correct? If yes, show the output otherwise first correct
them and then write their outputs.
a) System.out.printlm("25-4 is” + 25/4);
b) System.out.println(“25/4.0 is ” + 25/4.0);
Explanation: no error in this one, this statement is written without any errors and
would display the output user want, just put a space between is so your answer would
not be displayed after is and it will become confusing.
OUTPUT: 25/4.0 is 6.25
Explanation: no error in this one, this statement is written without any errors and
would display the output user want, just put a space between is so your answer would
not be displayed after is and it will become confusing the ouput will be in int as there
is no floating point number to store float values..
OUTPUT: 3*2/4 is 1
Explanation: no error in this one, this statement is written without any errors and
would display the output user want, just put a space between is so your answer would
not be displayed after is and it will become confusing the ouput will be in float as
there is 3.0 floating point number to store float values..so this result in 6.0 / 4 which is
1.5.
OUTPUT: 3.0*2/4 is 1.5
Explanation: there is error in this one, this statement is written with many erros as we
have used F for float when its small plus there is no need to use f float value convert
when we are writing values in float the calculation will be in float. And put space after
is to not get confused with the output.
Correction:
OUTPUT: 1.0F / 3.0F is 0.333333334
(Question # 15)
How many accurate digits are stored in a float or double type variable?
ANSWER:
float value has 7-8 number of accurate digits , and is 32 bit float/
double value has 15-17 number of accurate digits, is a 64 bit double floating point
in which 1 bit is for sign, 11 bits are for exponent and 52 bits for the value.
(Question # 16)
(Question # 17)
(Question # 18)
(Question # 19)
(Question # 20)
(Question # 21)
(Question # 22)
(Question # 23)
a) int i=10;
Correction:
There is a error in this code that instead of i it is written in third line 1 which is error.
Also quotation is ising from left side and the name of second variable is written wrong it
should be numNew. After fixing the error output is:
int i=10;
Output:
Outout:
0.50000000000000001
c) System.out.println(1.0 - 0.9);
Output:
0.09999999999999998
d) double x= 1.0;
double y= 5.0;
System.out.printl(x+ y+z);
Output:
13.0
e) float f= 12.5F;
int i= (int)f;
System.out.println(“f is ” + f);
System.out.println(“I is ”+ i);
Output:
f is 12.5
I is 12
f) public static void main( String args[] ) { int p=5, n=12 ; float r=2.0, si ; si = p * n
* r / 100 ;
Output:
1.200000
f) public static void main( String args[] ) { int num=2 ; if ( num <= 10 )
System.out. printf ( "What an obedient servant you are !" ) ; }
Output:
The error in this code is that in main method the bracket should be after String not after
args.
h) public static void main( String args[] ) { int qty=250, dis = 0 ; float rate=150, tot
=2500 ; if ( qty > 1000 ) dis = 10 ;
tot = ( qty * rate ) - ( qty * rate * dis / 100 ) ; System.out. printf ( "Total expenses =
Rs. %f", tot ) ; }
Output:
Error in the code no brackets after string and bracket after args should be removed.
i) public static void main( String args[] ) { int a = 300, b, c ; if ( a >= 400 ) b =
300 ; c = 200 ; System.out. printf ( "\n%d %d", b, c ) ; }
Output:
300 200
boolean z;
y = x = 10 ;
z = x < 10 ;
Output:
k) }
l) public static void main( String args[] ) { int k = 35 ; System.out. printf ( "\n%d
%d %d", k == 35, k = 50, k > 40 ) ; }
Output:
35 50 40
The error in the code is %d format specifier cannot be used for Boolean values since its
function us wth int values.
(Question # 24)
(Question # 25)
(Question # 26)
Output:
TRUE
OR
FALSE
Output: +2502
Output: |1234.1234
Output: 10 % 4 = 2
l) e= 123.1234;
double e = 123.1234;
m) Int x=1245;
int x=1245;
Correction: The value we have given to n is large for long s we have to assign data
type int to n to work.
int n= 461012;
Output: +461012
Correction: the error in this code is that pi is a constant value so we will convert it tp
constant value.
Output:
3.142
3.142
z=x*y;
s=x+y;
q) Int i=88;
System.out.printf ("%010d", i);
Correction: The datatype int should start with small i and second print statement
quotation mark on right side is missing.
int i=88;
Output:
0000000088value in 10 digits:88
Correction: since we are using format specifier so we have to use printf instead of
println and also double quote from left ride is missing from the print statement.
Output:
1234567890
Correction: while assigning a string value to variable the value should be in quotation
to tell the variable it’s a string value given by user. And also string s is capital while
initializing should be smaller.
System.out.println("First Name: " + firstname + " Last Name: "+ lastname +".");
Output:
Output:
Welcome to
Java programming
sum= a+b+c;
Correction: The error in ths code is that sum is not declared but is being used in the
code.
sum= a+b+c;
Output:
Sum is 30
v) int num1=5;
int num2=6;
if (num1 !=num2)
Correction:
Output: