Review Questions for Exam 10182016 1 public class Question1
Review Questions for Exam
10/18//2016
1.
public class Question1
{
public static void main(String[ ] args)
{
System.out.print(\"Here\");
System.out.println(\"There \" + \"Everywhere\");
System.out.println(\"But not\" + \"in NJ\");
}
}
Write the output after the above program is executed.
2. Consider the following statement:
System.out.println(\"1 big bad wolf\\t8 the 3 little pigs\ 4 dinner\ 2night\");
This statement will output ________ lines of text
A) 1
B) 2
C) 3
D) 4
E) 5
public class MyString
{
public static void main(String[] args)
{
System.out.print(\"1 big bad wolf\\t8 the 3 little pigs\ 4 dinner\ 2 night\");
}
}
3. The word println is a(n)
A) method
B) reserved word
C) variable
D) class
E) String
4. A Java variable is the name of a
5. What output is produced by the following?
System.out.println (\"X: \" + 25);
System.out.println (\"Y: \" + (15 + 50));
System.out.println (\"Z: \" + 300 + 50);
Of the following types, which one cannot store a numeric value?
6. What is output with the statement System.out.println(\"\"+x+y); if x and y are int values where x=10 and y=5?
public class MyString
{
public static void main(String[] args)
{
int x=10;
int y = 5;
System.out.println(\"\"+x+y);
}
}
7. What happens if you attempt to use a variable before it has been initialized?
A. A syntax error may be generated by the compiler
B. A runtime error may occur during execution
C. A \"garbage\" or \"uninitialized\" value will be used in the computation
D. A value of zero is used if a variable has not been initialized
E. Answers A and B are correct
8. What is the function of the dot operator?
A. It serves to separate the integer portion from the fractional portion of a floating point number
B. It allows one to access the data within an object when given a reference to the object
C. It allows one to invoke a method within an object when given a reference to the object
D. It is used to terminate commands (much as a period terminates a sentence in English)
E. Both B and C are correct
9. Say you write a program that makes use of the Random class, but you fail to include an import statement for java.util.Random (or java.util.*). What will happen when you attempt to compile and run your program.
A. The program won\'t run, but it will compile with a warning about the missing class.
B. The program won\'t compile-you\'ll receive a syntax error about the missing class.
C. The program will compile, but you\'ll receive a warning about the missing class.
D. The program will encounter a runtime error when it attempts to access any member of the Random class
E. none of the above
10. An API is
A. an Abstract Programming Interface
B. an Application Programmer\'s Interface
C. an Application Programming Interface
D. an Abstract Programmer\'s Interface
E. an Absolute Programming Interface
11. When executing a program, the processor reads each program instruction from
12. The main method for a Java program is defined by
13. What will be the result of the following assignment statement? Assume b = 5 and c = 10.
int a = b * (-c + 2) / 2;
public class MyString
{
public static void main(String[] args)
{
int b = 5;
int c = 10;
int a = b * (-c + 2) / 2;
System.out.println(\"a: \"+a);
}
}
14. If the String major = \"Computer Science\", what is returned by major.charAt(1)?
public class MyString
{
public static void main(String[] args)
{
String major = \"Computer Science\";
System.out.println(major.charAt(1));
}
}
15. For the following questions, refer to the class defined below:
import java.util.Scanner;
public class Questions15
{
public static void main(String[ ] args)
{
int x, y, z;
double average;
Scanner scan = new Scanner(System.in);
System.out.println(\"Enter an integer value\");
x = scan.nextInt( );
System.out.println(\"Enter another integer value\");
y = scan.nextInt( );
System.out.println(\"Enter a third integer value\");
z = scan.nextInt( );
average = (x + y + z) / 3;
System.out.println(\"The result of my calculation is \" + average);
}
}
16. What is the output of the following program?
public class Precision
{
public static void main(String[] args)
{
int x=5;
int y =5;
int z = 3;
double average;
average = (x + y + z) / 3;
System.out.println(\"The result of my calculation is \" + average);
}
}
ANS: ÏYour gross pay is $1000.0
17. What is the output of the following program?
public class Initialize
{
// This program shows variable initialization.
public static void main(String[] args)
{
int month = 2, days = 28;
System.out.println(\"Month \" + month + \" has \" +
days + \" days.\");
}
}
18. What is the output of the following program?
public class Payroll
{
public static void main(String[] args)
{
int hours = 40;
double grossPay, payRate = 25.0;
grossPay = hours * payRate;
System.out.println(\"Your gross pay is $\" + grossPay);
}
}
19.
import java.util.Scanner; // Needed for the Scanner class
/*
This program has a problem reading input.
*/
public class InputProblem
{
public static void main(String[] args)
{
String name; // To hold the user\'s name
int age; // To hold the user\'s age
double income; // To hold the user\'s income
// Create a Scanner object to read input.
Scanner keyboard = new Scanner(System.in);
// Get the user\'s age.
System.out.print(\"What is your age? \");
age = keyboard.nextInt();
// Get the user\'s income
System.out.print(\"What is your annual income? \");
income = keyboard.nextDouble();
// Get the user\'s name.
System.out.print(\"What is your name? \");
name = keyboard.nextLine();
// Display the information back to the user.
System.out.println(\"Hello \" + name + \". Your age is \" +
age + \" and your income is $\" +
income);
}
}
What is your age? 50
¼¼§ÏWhat is your annual income? 1000
ÏϧÏWhat is your name? Hello . Your age is 50 and your income is $1000.0
To fix the problem, add this line before the statement of System.out.print(\"What is your name? \");
// Consume the remaining newline.
keyboard.nextLine();
20. What is the output of the following program?
// This program demonstrates the char data type.
public class Letters
{
public static void main(String[] args)
{
char letter;
letter = \'A\';
System.out.println(letter);
letter = \'B\';
System.out.println(letter);
}
}
21. Write an expression that produces a random integer in the following ranges:
Range
0 to 12
1 to 20
22.
public class MyString
{
//-----------------------------------------------------------------
// Prints a string and various mutations of it.
//-----------------------------------------------------------------
public static void main(String[] args)
{
String str1=\"Two\";
System.out.println( str1 );
int count=str1.length();
System.out.println(count);
System.out.println( str1.length() );
String str3 = str1.substring(1,3);
System.out.println( str3 );
String str2= new String (\"Three\");
String str6= new String (\"THREE\");
boolean equalThree1 = str2.equals(str6);
boolean equalThree2 = str2.equalsIgnoreCase(str6);
String str4= str2.concat(\" Four\");
String str5=str2.replace(\'e\', \'x\');
System.out.println( str2 );
System.out.println( str4 );
System.out.println( str5 );
System.out.println( equalThree1 );
System.out.println( equalThree2 );
String str7=\"AB\";
String str8=\"CD\";
System.out.println( str7.compareTo(str8) );
System.out.println( str8.compareTo(str7) );
System.out.println (str7.charAt(0));
}
}
23.
Trace the execution of the following program assuming the input stream contains the numbers 10, 3, and 14.3. Use a table that shows the value of each variable at each step. Also show the output (exactly as it would be printed).
// FILE: Trace.java
// PURPOSE: An exercise in tracing a program and understanding
// assignment statements and expressions.
import java.util.Scanner;
public class Trace
{
public static void main (String[] args)
{
int one, two, three; //line 1
double what; //line 2
Scanner scan = new Scanner(System.in); //line 3
System.out.print (\"Enter two integers: \"); //line 4
one = scan.nextInt(); //line 5
two = scan.nextInt(); //line 6
System.out.print(\"Enter a floating point number: \"); //line 7
what = scan.nextDouble() ; //line 8
three = 4 * one + 5 * two; //line 9
two = 2 * one; //line 10
System.out.println (\"one \" + two + \":\" + three); //line 11
one = 46 / 5 * 2 + 19 % 4; //line 12
three = one + two; //line 13
what = (what + 2.5) / 2 ; //line 14
System.out.println (what + \" is what!\"); //line 15
}
}
24. Write an application that reads values representing a time duration in hours, minutes, and seconds and then prints the equalivant total number of seconds. (for example, 1 hour, 28 minutes and 42 seconds is equivalent to 5322 seconds.)
Solution
Hi, Friend please do not post more than 5 questions in a single post.
Please find my answer for first 6 questions.
Please post other questions in other post.
1)
HereThere Everywhere
But notin NJ
Explanation:
System.out.println => peint the argument on TERMINAL
+ => concatenate two string OR one string and other is any data type
2)
Output:
1 big bad wolf 8 the 3 little pigs
4 dinner
2night
Line break with \ and \ character
Ans: 3
3)
println is a method of System class
Ans: ) method
4)
c.data value stored in memory that can not change its type during the program’s execution
5)
X: 25
Y: 65
Z: 30050
6)
Ans: 105
System.out.println(\"\"+x+y); -=> \"\"+x = 10 that is string
10(string) + 5(int) => 105 (string)
![Review Questions for Exam 10/18//2016 1. public class Question1 { public static void main(String[ ] args) { System.out.print(\ Review Questions for Exam 10/18//2016 1. public class Question1 { public static void main(String[ ] args) { System.out.print(\](/WebImages/32/review-questions-for-exam-10182016-1-public-class-question1-1092831-1761575710-0.webp)
![Review Questions for Exam 10/18//2016 1. public class Question1 { public static void main(String[ ] args) { System.out.print(\ Review Questions for Exam 10/18//2016 1. public class Question1 { public static void main(String[ ] args) { System.out.print(\](/WebImages/32/review-questions-for-exam-10182016-1-public-class-question1-1092831-1761575710-1.webp)
![Review Questions for Exam 10/18//2016 1. public class Question1 { public static void main(String[ ] args) { System.out.print(\ Review Questions for Exam 10/18//2016 1. public class Question1 { public static void main(String[ ] args) { System.out.print(\](/WebImages/32/review-questions-for-exam-10182016-1-public-class-question1-1092831-1761575710-2.webp)
![Review Questions for Exam 10/18//2016 1. public class Question1 { public static void main(String[ ] args) { System.out.print(\ Review Questions for Exam 10/18//2016 1. public class Question1 { public static void main(String[ ] args) { System.out.print(\](/WebImages/32/review-questions-for-exam-10182016-1-public-class-question1-1092831-1761575710-3.webp)
![Review Questions for Exam 10/18//2016 1. public class Question1 { public static void main(String[ ] args) { System.out.print(\ Review Questions for Exam 10/18//2016 1. public class Question1 { public static void main(String[ ] args) { System.out.print(\](/WebImages/32/review-questions-for-exam-10182016-1-public-class-question1-1092831-1761575710-4.webp)
![Review Questions for Exam 10/18//2016 1. public class Question1 { public static void main(String[ ] args) { System.out.print(\ Review Questions for Exam 10/18//2016 1. public class Question1 { public static void main(String[ ] args) { System.out.print(\](/WebImages/32/review-questions-for-exam-10182016-1-public-class-question1-1092831-1761575710-5.webp)
![Review Questions for Exam 10/18//2016 1. public class Question1 { public static void main(String[ ] args) { System.out.print(\ Review Questions for Exam 10/18//2016 1. public class Question1 { public static void main(String[ ] args) { System.out.print(\](/WebImages/32/review-questions-for-exam-10182016-1-public-class-question1-1092831-1761575710-6.webp)
![Review Questions for Exam 10/18//2016 1. public class Question1 { public static void main(String[ ] args) { System.out.print(\ Review Questions for Exam 10/18//2016 1. public class Question1 { public static void main(String[ ] args) { System.out.print(\](/WebImages/32/review-questions-for-exam-10182016-1-public-class-question1-1092831-1761575710-7.webp)