Download Practice_Questions

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Page 1 of 4
LECTURE 01 - Introduction to Java
Practice Questions
Q. Write Java statements to accomplish each of the following tasks:
a) Declare variables c, thisIsAVariable, q76354 and number to be of type int.
ANS:
int c, thisIsAVariable, q76354, number;
or
int c;
int thisIsAVariable;
int q76354;
int number;
b) Display a message asking the user to enter an integer and assign the result to String variable value
ANS:
Scanner sc = new Scanner(System.in)
String value = sc.next();
c) Print "This is a Java program" on one line in the command window.
ANS:
System.out.println( "This is a Java program" );
f) Print "This is a Java program" on two lines in the command window; the first line should end with Java. Use only one
statement.
ANS:
System.out.println( "This is a Java\nprogram" );
Q. Write declarations, statements or comments that accomplish each of the following tasks:
a) State that a program will calculate the product of three integers.
ANS:
// Calculate the product of three integers
b) Declare the variables x, y, z and result to be of type int.
ANS:
int x, y, z, result;
or
int x;
int y;
int z;
int result;
c) Prompt the user to enter the first value, read the value from the user and store it in the variable xVal.
ANS:
Scanner sc = new Scanner(System.in);
System.out.println(( "Enter first integer:" );
xVal = sc.nextInt();
j) Compute the product of the three integers contained in variables x, y and z, and assign the result to the variable result.
ANS:
result = x * y * z;
k) Display a message "The product is " followed by the value of the variable result.
ANS:
System.out.println(( "The product is "+result );
Page 2 of 4
Q. What println statements will generate this output?
This program prints a
quote from the Gettysburg Address.
"Four score and seven years ago,
our 'fore fathers' brought forth on
this continent a new nation."
ANS:
System.out.println("This program prints a");
System.out.println("quote from the Gettysburg Address.");
System.out.println();
System.out.println("\"Four score and seven years ago,");
System.out.println("our 'fore fathers' brought forth on");
System.out.println("this continent a new nation.\"");
Q. Product.java
Write a program that prompts the user to enter 3 numbers and prints the roduct of the three
ANS:
import java.util.Scanner;
public class Product {
public static void main( String args[] )
{
int x; // first number
int y; // second number
int z; // third number
int result; // product of numbers
Scanner sc = new Scanner(System.in);
System.out.println( "Enter first integer:" );
x = sc.nextInt();
System.out.println( "Enter second integer:" );
y = sc.nextInt();
System.out.println( "Enter thirs integer:" );
z = sc.nextInt();
result = x * y * z;
System.out.println("The product is " + result );
} // end method main
} // end class Product
Page 3 of 4
Q. Calculate.java
Write an application that asks the user to enter two numbers, obtains the numbers from the
user and prints the sum, product, difference and quotient (division) of the numbers.
Sample run:
ANS:
import java.util.Scanner;
public class Calculate {
public static void main( String args[] )
{
Scanner sc = new Scanner(System.in);
int number1, number2;
int sum, product, difference, quotient;
// read first number from user
System.out.println("Enter first integer:");
number1 = sc.nextInt();
// read second number from user
System.out.println("Enter second integer:");
number2 = sc.nextInt();
// calculate
sum = number1 + number2;
product = number1 * number2;
difference = number1 - number2;
quotient = number1 / number2;
// display results
System.out.println( "The sum is " + sum );
System.out.println( "The product is " + product );
System.out.println( "The difference is " + difference );
System.out.println( "The quotient is " + quotient );
}
}
Page 4 of 4
Q. Circle.java
Write an application that inputs from the user the radius of a circle as an integer and prints the circle’s
diameter, circumference and area. Use the value 3.14159 for π.
[Note: You may also use the predefined constant Math.PI for the value of π. This
constant is more precise than the value 3.14159. Class Math is defined in the java.lang package,
so you do not need to import it.] Use the following formulas (r is the radius):
diameter = 2r
circumference = 2πr
area = πr2
Do not store the results of each calculation in a variable. Rather, add the result of each directly to a
string that will be used to display the results.
Sample run:
ANS:
import java.util.Scanner;
public class Circle {
public static void main( String args[] )
{
Scanner sc = new Scanner(System.in);
String result; // output display string
int radius; // radius of circle
// read from user as a string
System.out.println("Enter radius:");
radius = sc.nextInt();
result = "Diameter is " + ( 2 * radius ) +
"\nArea is " + ( Math.PI * radius * radius ) +
"\nCircumference is " + ( 2 * Math.PI * radius );
// Display results
System.out.println( result);
}
}