0% found this document useful (0 votes)
330 views

Assignment 2

The document contains an assignment with 14 questions related to Java programming. It asks the student to write Java code to declare variables, take user input, perform arithmetic operations, print shapes using asterisks, and identify errors. The answers section provides the code to solve each question.

Uploaded by

Hassan Khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
330 views

Assignment 2

The document contains an assignment with 14 questions related to Java programming. It asks the student to write Java code to declare variables, take user input, perform arithmetic operations, print shapes using asterisks, and identify errors. The answers section provides the code to solve each question.

Uploaded by

Hassan Khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Assignment # 01

Batch 2018 – CED Due Date: 16th July, 2018


Note: Submit the assignment on time and in print out format in separate files.
Q1: Write statements to accomplish each of the following tasks:
a) Declare variables c, thisIsAVariable, q76354 and number to be of type int.
b) Prompt the user to enter an integer.
c) Input an integer and assign the result to int variable value. Assume Scanner variable input
can be used to read a value from the keyboard.
d) Print "This is a Java program" on one line in the command window. Use method
System.out.println.
e) Print "This is a Java program" on two lines in the command window. The first line should
end with Java. Use method System.out.println.
f) Print "This is a Java program" on two lines in the command window. The first line should
end with Java. Use method System.out.printf and two %s format specifiers.
g) If the variable number is not equal to 7, display "The variable number is not equal to 7".
Q2: Identify and correct the errors in each of the following statements:
a) if ( c < 7 );
System.out.println( "c is less than 7" );
b) if ( c => 7 )
System.out.println( "c is equal to or greater than 7" );
Q3: Assuming that x = 2 and y = 3, what does each of the following statements display?
a) System.out.printf( "x = %d\n", x );
b) System.out.printf( "Value of %d + %d is %d\n", x, x, ( x + x ) );
c) System.out.printf( "x =" );
d) System.out.printf( "%d = %d\n", ( x + y ), ( y + x ) );
Q4: Which of the following Java statements contain variables whose values are modified?
a) p = i + j + k + 7;
b) System.out.println( "variables whose values are modified" );
c) System.out.println( "a = 5" );
d) value = input.nextInt();
Q5: Given that y = ax3 + 7, write the equivalent Java statements for this equation?
Q6: 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;
b) x = 2 % 2 + 2 * 2 - 2 / 2;
c) x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) );
Q7: Write an application that displays the numbers 1 to 4 on the same line, with each pair of
adjacent numbers separated by one space. Use the following techniques:
a) Use one System.out.println statement.
b) Use four System.out.print statements.
c) Use one System.out.printf statement.
Q8: (Arithmetic) Write an application that asks the user to enter two integers, obtains them
from the user and prints their sum, product, difference and quotient (division).
Q9: (Displaying Shapes with Asterisks) Write an application that displays a box, an oval, an arrow
and a diamond using asterisks (*), as follows:

Q10: What does the following code print?


System.out.println( "*\n**\n***\n****\n*****" );
Q11: What does the following code print?
System.out.println( "*" );
System.out.println( "***" );
System.out.println( "*****" );
System.out.println( "****" );
System.out.println( "**" );
Q12: What does the following code print?
System.out.print( "*" );
System.out.print( "***" );
System.out.print( "*****" );
System.out.print( "****" );
System.out.println( "**" );
Q13: What does the following code print?
System.out.print( "*" );
System.out.println( "***" );
System.out.println( "*****" );
System.out.print( "****" );
System.out.println( "**" );
Q14: What does the following code print?
System.out.printf( "%s\n%s\n%s\n", "*", "***", "*****" );
ANSWERS
Q1) PART A:
int c , thisisavariable , q76354 , number ;

Q1) PART B:
import java.util.Scanner;
Scanner a = new Scanner(System.in);
System.out.println("Enter an integer: ");
int b = a.nextInt();

Q1) PART C:
import java.util.Scanner;
Scanner input = new Scanner(System.in);
Int value = input.nextInt();

Q1) PART D:
System.out.println("This is a Java Program ");

Q1) PART E:
System.out.println("This is a Java \nProgram ");

Q1) PART F:
System.out.printf("%s\n%s",”This is java”,”program”);

Q1) PART G:
if ( number != 7 )
System.out.println( "The variable number is not equal to 7." );
Q2) PART A:
No error identified.

Q2) PART B:
Greater than sign (>) comes before is equal to sign (=).
if ( c >= 7 )
System.out.println( "c is equal to or greater than 7" );

Q3) PART A:
x=2

Q3) PART B:
Value of 2+2 is 4

Q3) PART C:
x=

Q3) PART D:
5=5

Q4)
a) p = i + j + k + 7;
d) value = input.nextInt();
Q5)
y=a *x*x*x+7;

Q6) PART A:
- and + operations are performed first but /, % and * have same precedence.
x =7 + 3 * 6 / 2 – 1 = 15

Q6) PART B:
- and + operations are performed first but /, % and * have same precedence.
x=2%2+2*2-2/2=3

Q6) PART C:
- and + operations are performed first but /, % and * have same precedence.
x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) ) = 324

Q7) PART A:
System.out.print("1 2 3 4");

Q7) PART B:
System.out.print(" 1 ");
System.out.print(" 2");
System.out.print(" 3 ");
System.out.printf(" 4");

Q7) PART C:
System.out.println(" 1 2 3 4");
Q8)
Scanner a=new Scanner(System.in);
System.out.print("Enter Any Two Intergers: ");
int num1=a.nextInt();
int num2=a.nextInt();
int num3=(num1+num2);
int num4=(num1-num2);
int num5=(num1*num2);
float num6=(num1/num2);
System.out.println("Addition of two integers: "+num3);
System.out.println("Difference of two integers: "+num4);
System.out.println("Product of two integers: "+num5);
System.out.printf("Quotient of two integers: %.2f",num6);

Q9)
System.out.print(" ********** *** * *\n");
System.out.print(" * * * * *** * *\n");
System.out.print(" * * * * ***** * *\n");
System.out.print(" * * * * * * *\n");
System.out.print(" * * * * * * * \n");
System.out.print(" * * * * * * *\n");
System.out.print(" * * * * * * *\n");
System.out.print(" * * * * * * *\n");
System.out.print(" * * * * * * *\n");
System.out.print(" ********** *** * *\n");
Q10)
*
**
***
****
*****

Q11)
*
***
*****
****
**

Q12)
***************

Q13)
****
*****
******

Q14)
*
***
*****

You might also like