PF Assignment 1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 28

Convert the following into Java Statements & write order precedence:

a) 3+4𝑥 5 − 10(𝑦−5)(𝑎+𝑏+𝑐)𝑥 +9(4𝑥+9+𝑥𝑦)?

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)

b) g = big / 2 + big * 4 / big - big + abc / 3 ; (abc = 2.5, big = 2, assume g to be a


float)

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 = 1 + 2 * 4 / 2 – 2 + 2.5 / 3 (Left most Multiplication)

g = 1 + 8 / 2 – 2 + 2.5 / 3 (Left most Division)

g = 1 + 4 – 2 + 2.5 / 3 (Division Operator)

g = 1 + 4 – 2 + 0.83333 (Left most Addition)

g = 5 – 2 + 0.83333 (Left most Substraction)

g = 3 + 0.83333 (Addition)

g = 3.83333 (OUTPUT)

c) on = ink * act / 2 + 3 / 2 * act + 2 + tig ; (ink = 4, act = 1, tig = 3.2, assume on to

be an int)?

on = ink * act / 2 + 3 / 2 * act + 2 + tig (Put values of ink, act and tig)

on = 4 * 1 / 2 + 3 / 2 * 1 + 2 + 3.2 (Left most Multiplication)

on = 5 / 2 +3 / 2 * 1 + 2 + 3.2 (Left most Division)

on = 2.5 + 3 / 2 * 1 + 2 + 3.2 (Left most Division)

on = 2.5 + 1.5 * 1 + 2 + 3.5 (Multiplication Operator)

on = 2.5 + 1.5 + 2 + 3.5 (Left most Addition)

on = 4 + 2 + 3.5 (Left most Addition)

on = 6 + 3.5 (Addition Operator)

on = 9.5 (OUTPUT)

d) s = qui * add / 4 - 6 / 2 + 2 / 3 * 6 / god; (qui = 4, add = 2, god = 2, assume s to be


an int)

s = qui * add / 4 - 6 / 2 + 2 / 3 * 6 / god (Put values of qui, add , god)

s=4*2/4–6/2+2/3*6/2 (Left most Multiplication)


s=8/4–6/2+2/3*6/2 (Left most Division)

s=2–6/2+2/3*6/2 (Left most Division)

s=2–3+2/3*6/2 (Left most Division)

s = 2 – 3 + 0.6666 * 6 / 2 (Multiplication Operator)

s=2–3+4/2 (Left most Substraction)

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=1/3*a/4-6/2+2/3*6/g (Put values of a and g)

s=1/3*4/4-6/2+2/3*6/3 (Left most Division)

s = 0.333 * 4 / 4 - 6 / 2 + 2 / 3 * 6 / 3 (Left most Multiplication)

s = 1.333 / 4 - 6 / 2 + 2 / 3 * 6 / 3 (Left most Division)

s = 0.333 - 6 / 2 + 2 / 3 * 6 / 3 (Left most Division)

s = 0.333 - 3 + 2 / 3 * 6 / 3 (Left most Division)

s = 0.333 - 3 + 0.666 * 6 / 3 (Left most Multiplication)

s = 0.333 - 3 + 4 / 3 ( Division)

s = 0.333 - 3 + 1.333 (Left most Substraction)

s = -2.6667 + 1.333 (Addition)

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 ) ) ) );

1) bracket -1, 2) bracket-2 * , / , 3) bracket-3 +, 4) bracket-4 *,*

x=(3*9*(3+(9*3/(3)))) (Bracket 1)

x=(3*9*(3+(9*3/ 3 ))) (Bracket 2, Multiplication)

x = ( 3 * 9 * ( 3 + ( 27 / 3 ) ) ) (Bracket 2, Division)

x=(3*9*(3+9 )) (Bracket 3 , Addition)

x = ( 3 * 9 * 12 ) (Left most Multiplication)

x = ( 27 * 12 ) (Multiplication)

x = 324 (OUTPUT)

(Question # 5)

What does the following code print?


System.out.println( "*" );
System.out.println( "***" );
System.out.println( "*****" );
System.out.println( "****" );
System.out.println( "**" );

OUTPUT:

*
***
*****
****
**

(Question # 6)

What does the following code print?


System.out.print( "*" );
System.out.print( "***" );
System.out.print( "*****" );
System.out.print( "****" );
System.out.println( "**" );

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)

Identify and fix errors in the following code:

a) public class Test {

public void main (string [] args) {


double i= 50.0;

double k = 1 + 50.0;

double j – k + 1;

system.out.println(“j is ” + j + “ and K is ” + k);

Errors:

static keyweord missing from line 2.


string s should be capital in line 2.
System s should be capital.
Equal sign missing in line 5

Correction:

public class Test {


public void static main (String [] args) {
double i= 50.0;
double k = 1 + 50.0;
double j= k + 1;
System.out.println(“j is ” + j + “ and K is ” + k);
}
}

Output:

j is 52.0 and K is 51.0

b) public class ShowLogicErrors

public static void main(String[] args) {

System.out.println("Celsius 35 is Fahrenheit degree ");

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

public static void main(String[] args) {

System.out.println("Celsius 35 is Fahrenheit degree ");

System.out.println((9 / 5) * 35 + 32);

Output:

Celsius 35 is Fahrenheit degree

c) public class ShowRuntimeErrors {

public static void main(String[] args) {

System.out.println(1 / 0);

}}

Error: cannot use ShowRuntimeError name class

Correction:

public class Error{

public static void main(String[] args) {

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{

public static void main(String[] args){

System.out.println(“Problem Driven”);

}}

Output:

Problem Driven

e) public class Test {

Error: No main method or brackets cause error.

Correction:

public class Test{

public static void main(String[] args){

}}

No output at it doesn’t have any print statement

f) public void main(string[] args) {

double i = 50.0;

double k = i + 50.0;

double j = k + 1;

System.out.println("j is " + j + " and k is " + k); } }

Error: static keyword is missing and string s is small and no class in the beginning.

Correction:

public class Temp{

public static void main(String[] args) {

double i = 50.0;

double k = i + 50.0;
double j = k + 1;

System.out.println("j is " + j + " and k is " + k); } }

Output:

j is 51.0 and k is 52.0

g) { if ( 'X' < 'x' )

System.out. printf ( "\nascii value of X is smaller than that of x" ) ; }

Error: No class at start, no main method and also we use %n for escape character in
printf statement.

Correction:

public class Checking{

public static void main (string[] args){

if ( 'X' < 'x' )

System.out. printf ( "%nascii value of X is smaller than that of x" ) ; }}

Output:

ascii value of X is smaller than that of x

g) public static void main( String args[] ) {

int x = 10 ;

if ( x >= 2 )

System.out. printf ( "\n%d", x ) ;

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{

public static void main( String args[] ) {

int x = 10 ;
if ( x >= 2 )

System.out. printf ( "%n%d", x ) ; }}

Output:

10

h) public static void main( String args[] ) {

int x = 10, y = 15 ;

if ( x % 2 = y % 3 )

System.out. printf ( "\nCarpathians" ) ; }

Error: No class main method and in line 3 should == operator to check euality.

Correction:

Class Check{

public static void main( String args[] ) {

int x = 10, y = 15 ;

if ( x % 2 == y % 3 )

System.out. printf ( "\nCarpathians" ) ; }}

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.

METHOD 1: float (25/4)


In this method it will fisrt be evaluated and the answer will be then converted into
floating value so this method is calculating first and then changing it into your own
required data type form.
METHOD 2: Rewrite 25/4 as 25.0 / 4.0 , 25.0 / 4 , 25/ 4.0 and (25f) / (4f) = 6.24 would
give output of 25/4 into floating point value. Any of these can be used to get point
answers.

(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 is 6 (First display the 25/4 is and then used catenation symbol + to


convert the non string value into string so it divide the values
and convert it to string and display it in output.)

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)

Show the output of the following code:

Part (a): Output


0 2 0.000000 2.000000
The ouput of the following code is written above in the print statement we have use for
format specifier %d two time and then %f which state is fisrt two value will be in int
and last two will be in float values.

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);

Explanation: The following statement is correct id we are checking in term of this


code executing then its correct as it will be compiled and executed with a output.
But it is not logical correct as we have written inside the quotation that 25-4 and we
are dividing the values in the evaluation so we have to correct this to get our desired
answer wich logically is correct. Another error is that instead of println its written
printlm which is also a syntax error so code will not run due to this error for other
error it will run but answer would not be what the user expected.
Correction: System.out.println(“25 – 4 is ”+ 25 - 4);
OUTPUT: 25-4 is 21

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

c) System.out.println(“3*2/4 is” + 3*2/4);

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

d) System.out.println(“3.0*2/4 is” + 3.0*2/4);

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

e) System.out.println(“1.0F / 3.0F is” + 1.0F/3.0F);

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)

Which of the following shows the correct hierarchy of arithmetic operators in


JAVA
(1) **, * or /, + or – (Incorrect as ** is not a arthematic operator.)
(2) **, *, /, +, - (Incorrect as ** is not a arthematic operator.)
(3) **, /, *, +, - (Incorrect as ** is not a arthematic operator.)
(4) / or *, - or + (correct hierarchy of arthematic operator in java.)

(Question # 17)

In b = 6.6 / a + 2 * n ; which operation will be performed first?


(1) 6.6 / a
(2) a + 2
(3) 2 * n
(4) Depends upon compiler
ANSWER: (1) the operator that is performed first will be 6.6 / a as we know that
if it’s a bracket in the expression then have to solve that first, but we donot have any
bracket here so we will left this rule and next rule is if you have multiplication or
division in a expression and we know that in Java both have same precedence level so
we will perform the operation with operator who is at left most side in this expression it
is division then cause we know that multiplication in predecedence order is evaluated
before addition or substraction, so we will then perform multiplication operation and in
the end addition.
b = 6.6 / a + 2 * n
(4) (1) (3) (2)

(Question # 18)

Which of the following shows the correct hierarchy of arithmetic operations in


JAVA
(1) / + * -
(2) * - / +
(3) + - / *
(4) * / + -
ANSWER: From all the above options the correct one is (4).
Explanation: Because we know that while performing athematic operations we have to
follow the precedence order which states bracket first then division and multiplication
and among these any one the operator can be executed which is at left side and after
evaluating multiplication and division operations we have to perform addition and
subtraction and for these two we follow the same rule which we do with the
multiplication and division operator, suppose we have a division, multiplication,
addition and subtraction operators in our expression so the operator which will be
executed first will be multiplication or division and among these we are going to
perform the operation which is left most side after performing both these operators we
will move to addition and subtraction and from these two we will solve the one who is
left most side and then will perform the last operation and our expression will be
executed with the right method that is followed by the Java.

(Question # 19)

Which of the following are correct literals for floating-point numbers?


12.3, 12.3e+2, 23.4e-2, -334.4, 20.5, 39F,40D?
ANSWER: All the values are correct literals for floating-point numbers.
Explanation: If we look at each of the value individually all of them are literal for
floating point values.
(1) 12.3 is of data type double and by using .3 make it floating point number.
(2) 12.3e+2 is also a floating-point number irrespective of its exponent it has a
fraction part .3 and it is also a double data type value.
(3) 23.4e-2, this value is also a floating-point number cause it have fraction part .4
and is also a double data type value.
(4) -334.4 is also double data type value with fraction part.
(5) 20.5 is also a correct literal for floating point number since it is in fraction and
it is also in double data type.
(6) 39F this value doesn’t have a fraction which doesn’t specify that it is not a
floating point number it is a floating point number because it has use a mark
or symbol F which states it is a floating value this F converts it into a floating
value by adding a fraction part and the value 39F is in float data type.
(7) 40D is also a double data type value it has a D written with it which changes
the value into double data type so this is also a correct form of literal for
floating point numbers.

(Question # 20)

Which of the following are same as 52.534?


5.2534e+1, 0.52534e+2, 52.34e-1, 5.2534e+0?
ANSWER: Following of the value are same as 52.534:
5.2534e+1
0.52534e+2
525.34e-1
When we take the exponent of these and run the code so answer will be 52.534.
5.2534e+1, 0.52534e+2 and 525.34e-1 is a method for writing 52.534.
5.2534e+0 is only the one that will not give this value 52.534 and this would actually be
equal to 5.2534e which is not correct

(Question # 21)

Which of the following are correct literals?


5_2534e+1, _2534, 5_2, 5
ANSWER:
(1) 5_2534e+1 is a correct literal and it is a valid double literal for representing
floating values.
(2) _2534 is an invalid literal cause it is starting with an underscore so it cannot be
literal but we can consider it as a variable name because it is using an underscore in the
beginning it will be valid variable name but not literal.
(3) 5_2 is a correct literal of int value an underscore can be used in between the
numbers so we can enhance the readability of the code and make it easier for the user
to read values but cannot put underscore in beginning or end.
(4) 5_ underscore cannot be used in the end of digit it should be in the middle of
numbers else it is wrong and not a correct literal.

(Question # 22)

Cane different types of numeric values be used together in a computation. What


does an explicit casting from a double to be an int do with the fractional part of
double value? Does casting the variable being cast?
ANSWER: Yes, different types of numeric values can be used together for
computation. We have seen many expressions with int and float values and are being
evaluating so for computation different data types values don’t have an effect.
When we compute different numeric values and are assigning it to some variable the
data type matters most there we can assign a value of smaller data type to large data type
for example from byte to int will work fine but we cannot do it in reverse such as from
int to byte it is not possible so java perform an automatic conversion on it.
When we will convert the values from double into int with the fractional the fraction
part of the double value will be removed and only int value will be stored in the int data
type suppose we have 5.23 double value converting it to int its value will be 5 the
fraction part will be removed. While converting form larger data type to smaller type we
have to be careful cause it causes the loss of data. We have to use data type according to
our requirements for the code.
No by performing casting does not change the variable which is being cast.

(Question # 23)

Write the ouput of the following:

a) int i=10;

int numNew = 10*(++1);

System.out.println(“I is “ + i +”, newNum is” + newNum);

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;

int numNew = 10*(++i);

System.out.println(“I is “ + i +”, numNew is” + numNew);

Output:

I is 10, numNew is 110

b) System.out.println(1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1);

Outout:

0.50000000000000001

c) System.out.println(1.0 - 0.9);

Output:

0.09999999999999998

d) double x= 1.0;
double y= 5.0;

double z = x-- +(++y);

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 ;

System.out. printf ( "%f" , si ) ; }

Output:

1.200000

We have to perform implicit casting on it by converting the r in byte.

f) public static void main( String args[] ) { int num=2 ; if ( num <= 10 )
System.out. printf ( "What an obedient servant you are !" ) ; }

Output:

What an obedient servant you are!

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:

Total expenses = Rs. 37500.000000

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

Error in the code no string brackets.

j) public static void main( String args[] ) { int x = 3, y ;

boolean z;

y = x = 10 ;

z = x < 10 ;

System.out. printf ( "\nx = %d y = %d z = %b", x, y, z ) ;

Output:

X=10 y=10 z= false

Errror in code no brackets after string in main method.

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)

Write the following Boolean expressions into Java statements:


a) True is weight is greater than 50 pounds or height is greater than 60 inches.
Java Statement: Boolean x= (weight > 50 ) || ( height > 60 )
The Boolean expression for this is to use operators such > for weight and height values
and then or operator.
b) True if weight is greater than 50 pounds and height is lesser than 60 inches.
Java Statement: Boolean x= (weight > 50 ) && ( height < 60)
The Boolean expression states weight is greater so we use operator > to show weight is
greater from this and then used & operator as in the expression it has used and word and
then measured height this all state that if all the given condition operators are executed
should be true value then

(Question # 25)

Which of the following are invalid variable names and why?


ANSWER:
INVALID variable names:
 #MEAN (A variable cannot start with a # symbol.)
 group. (Cannot use special character in variable name)
 422 (Cannot start variable name with a digit)
 Population in 2006 (Cannot use space between words use _)
 Over time (Cannot use space between word use _)
 queue. (Cannot use special character (.))
 team’svictory (Cannot use special character (‘) in variable.)
 2015_DDay (Variable name cannot start with a digit)
 basic-hra (Cannot use – between words use underscore.)
 plat#3 (cannot use # character can only use _ or $)

VALID variable names:


 BASICSALARY (Valid name for variable)
 _basic (underscore can be used to start variable name.)
 mindovermatter (words are written without space)
 FLOAT (not a reserve word can be used as variable name)
 hELLO (Valid name for variable)

(Question # 26)

Fix the errors and write output of following statements:

a) System.out.printf ("TRUE %nOR %nFALSE");


Correction: No error to correct, it has used %n as a escape sequence to print the next
word in next line.

Output:

TRUE

OR

FALSE

b) System.out.printf ("10 %% 4 = 2");


Correction: No error to correct it has used two modulus operators casue in Java to
display a character we have to % sign with it.
Output:
10 % 4 = 2

c) System.out.println("Hello! \f How are you all? ");

Correction: No error to correct

Ouptut: Hello! How are you all?

d) System.out.println("\\- this is a backslash. ");

Correction: No error to correct.

Output: \- this is a backslash.

e) System.out.println("Good Morning\t Geeks! ");

Correction: No error to correct.


Output: Good Morning Geeks!

f) System.out.println("Hi Learners, welcome to \"CUI\".");

Correction: No error to correct.

Output: Hi Learners, welcome to “CUI”.

g) System.out.printf( "%s\n%s\n%s\n", "*", "***", "*****" );

Correction: NO error to correct.


Output:
*
***
*****
h) System.out.printf(“%+d”, 2502);

Correction: No error to correct.

Output: +2502

i) System.out.printf ("|%-20.4f|", 1234.1234);

Correction: No error to correct

Output: |1234.1234

j) System.out.printf ("10 %% 4 = 2");

Correction: No error to correct.

Output: 10 % 4 = 2

k) System.out.printf ( "%E", 123.1234);

Correction: NO error to correct.


Output: 1.231234E+02

l) e= 123.1234;

System.out.println("UpperCase Scientific Notation" + " using %E: " + e);

Correction: e is not allocated a memory so first assign a data type to it.

double e = 123.1234;

System.out.printf("UpperCase Scientific Notation" + " using %E: " + e);

Output: UpperCase Scientific Notation using %E: 123.1234.

m) Int x=1245;

System.out.printf("Number: %o\n", x);

Correction: while declaring the variable it is written Int instead of int.

int x=1245;

System.out.printf("Number: %o\n", x);

Output: Numer: 2335


n) Long n= 461012;

System.out.printf ("%+8d%n", n);

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;

System.out.printf ("%+8d%n", n);

Output: +461012

o) Double pi= 3.142;

System.out.printf ("%.3f%n", pi);

System.out.printf ("%10.3f%n", pi);

Correction: the error in this code is that pi is a constant value so we will convert it tp
constant value.

final double PI= 3.142;

System.out.printf ("%.3f%n", PI);

System.out.printf ("%10.3f%n", PI);

Output:

3.142

3.142

p) int x=6, y =12, z;

z=x*y;

s=x+y;

System.out.println("Product= " + z + “and” +" Sum= "+ s +".");

Correction: no error to correct.

Output: Product=72 and Sum=18.

q) Int i=88;
System.out.printf ("%010d", i);

System.out.println ("value in 10 digits: + i);

Correction: The datatype int should start with small i and second print statement
quotation mark on right side is missing.

int i=88;

System.out.printf ("%010d", i);

System.out.println ("value in 10 digits:” + i);

Output:

0000000088value in 10 digits:88

r) System.out.println (%.10s", "12345678901234567890");

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

s) String firstname= Sara;

String lastname= Khan;

System.out.println("First Name: " + firstname + " Last Name: "+ lastname


+".");

System.out.println ("First Name: %s", firstname);

System.out.println ("Last Name: %s", lastname);

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.

string firstname= “Sara”;

string lastname= “Khan”;

System.out.println("First Name: " + firstname + " Last Name: "+ lastname +".");

System.out.println ("First Name: %s", firstname);


System.out.println ("Last Name: %s", lastname);

Output:

First Name: Sara Last Name: Khan.

First Name: Sara Last Name: Khan

t) System.out.printf("%s\n%s\n”, “Welcome to” , “ Java programming”);

Correction: No error to correct.

Output:

Welcome to

Java programming

u) int a=6, b=12, c=12;

sum= a+b+c;

System.out.printf(“Sum is %d\n”, sum);

Correction: The error in ths code is that sum is not declared but is being used in the
code.

int a=6, b=12, c=12, sum;

sum= a+b+c;

System.out.printf(“Sum is %d\n”, sum);

Output:

Sum is 30

v) int num1=5;

int num2=6;

if (num1 !=num2)

System.out.printf(“%d != %d \n”, num1, num2 );

Correction:

Output:

You might also like