Quiz
Untitled
Create a free account to finish this quiz
Save your progress and see your final score.
Already have an account? Sign in
Questions in this quiz (50)
1By default, a reference variable that is an instance field is initialized to the value
- Anull
- B0
- Cvoid
- Dstatic
2If you write a toString method for a class, Java will automatically call the method any time you concatenate an object of the class with a string.
- ATrue
- BFalse
3Of the following, which would be considered the no-arg constructor for the Rectangle class?
- Apublic Rectangle (int len, int width)
- Bpublic Rectangle (double len, double width
- Cpublic Rectangle()
- DAll of these
4The key word "this" is the name of a reference variable that an object can use to refer to itself.
- ATrue
- BFalse
5What will be returned from the method, if the following is the method header?
public Rectangle getRectangle()- Athe address of an object of the Rectangle class
- Bthe values stored in the data members of the Rectangle object
- Ca null value
- Dan object that is contained in the class Rectangle
6When a method's return type is an object, what is actually returned to the calling program?
- Aa reference to an object of that class
- Bonly the values in the object that the method accessed
- Can object of that class
- Da null reference
7When an object is passed as an argument, it is actually a reference to the object that is passed.
- ATrue
- BFalse
8Assume that inputFile references a Scanner object that was used to open a file. Which of the following while loops shows the correct way to read data from the file until the end of the file is reached?
- Awhile (inputFile.nextLine == " ")
- Bwhile (inputFile != null)
- Cwhile (! inputFile.EOF)
- Dwhile (inputFile.hasNext())
9Before entering a loop to compute a running total, the program should first
- Aset the accumulator variable to an initial value, usually zero
- Bset all variables to zero
- Cread all the values into main memory
- Dknow exactly how many values there are in total
10How many times will the following for loop be executed?
for(int count = 10; count <= 21; count++)
System.out.println("Java is great!");- A0
- B12
- C10
- D11
11What will be the value of x after the following code is executed?
int x=10;
while (x < 100)
{
x += 10;
}- A100
- B90
- C110
- D10
12What will be the value of x after the following code is executed?
int x = 10, y = 20;
while (y < 100)
{
x += y;
y += 20;
}- A130
- B210
- C110
- D90
13Which of the following statements opens a file named MyFile.txt and allows you to append data to its existing contents?
- AFileWriter fwriter = new FileWriter ("MyFile.txt);
PrintWriter outFile = new PrintWriter (fwriter); - BFileWriter fwriter = new FileWriter ("MyFile.txt", true);
PrintWriter outFile = new PrintWriter (fwriter); - CPrintWriter outfile = new PrintWriter ("MyFile.txt", true);
- DPrintWriter outfile = new PrintWriter (true, "MyFile.txt);
- AFileWriter fwriter = new FileWriter ("MyFile.txt);
14Which of the following statements opens a file named MyFile.txt and allows you to read data from it?
- AScanner inputFile = new Scanner ("MyFile.txt");
- BFile file = new File ("MyFile.txt");
Scanner inputFile = new Scanner (file); - CFile file = new File ("MyFile.txt");
- DPrintWriter inputFile = new PrintWriter ("MyFile.txt");
15When reading files using the Scanner class and the File class the Scanner class offers multiple hasNext() methods for various data types such as int, double, String, etc. For example, the hasNextInt() method returns true if an integer is available for reading, and false if the next item in the file is not an integer or if the end of the file has been reached.
- ATrue
- BFalse
16When done using the file, the program should leave the file open since there is no need to issue a close() method.
- ATrue
- BFalse
17The Scanner class can be used with System.in (to read from a keyboard) as well as a File object
- ATrue
- BFalse
18The File class is predefined and does not require an import statement
- ATrue
- BFalse
19Characters written to PrintWriter are places into a buffer, and the system outputs the buffer at various times.
- ATrue
- BFalse
20Because of the likelihood that a file fails to open, usually because the file does not exist of is in use by another program, the main() method definition contains a throws clause specifying that an exception of type OException may be thrown.
- ATrue
- BFalse
21A throws clause tells the Java Virtual Machine that the corresponding method may exit unexpectedly due to an exception (an input/output exception - IOException), such as trying to read from an input stream, or write to an output stream.
- ATrue
- BFalse
22What is the result of the following statement?
String s = "You" + "had" + "me + "at" + "hello";- AThe string s has the following value: "You had me at hello"
- BThe statement results in an error because the + operator can be used only with numbers
- CThe statement results in an error because the + operation cannot be performed on string literals
- DThe string s has the following value: "Youhadmeathello"
23What is the output produced by these statements?
String name = "Joanne Hunt";
System.out.println(name.length());- A8
- B10
- C9
- D11
24What is the output of the following code snippet?
String str1;
str1 = "I LOVE MY COUNTRY";
String str2 = str1.substring(4,11);
System.out.println(str2);- AOVE MY
- BOVE MY C
- CVE MY CO
- DVE MY C
25Which of the following statements can be used to get the fifth character from a string str?
- Achar c = str.charAt(5);
- Bchar c = str.charAt(4);
- Cchar c = str[5];
- Dchar c = str[4];
26What is the output of the following code snippet?
String str = "Java Is Good";
int n = str.length();
String mystery = str.substring(n - 4, n) + str.charAt(4) + str.substring(0, 4);
System.out.println(mystery);- AJava
- BGood Java
- CGood
- DIs Good
27What will be printed by the statements below?
int x = 20;
int y = x;
x += 3;
y += 2;
System.out.println("c = " + x + ", y = " + y);- Ax = 23, y = 46
- Bx = 23, y = 40
- Cx = 46, y = 46
- Dx = 3, y = 2
28At what point in the problem-solving process should one write pseudocode?
- AAfter writing Java code, as a way to summarize the code's algorithm
- BBefore writing Java code, as a guide for a general solution
- CAfter defining the variables so that the pseudocode and data types make sense
- DBefore working out examples by hand in order to guide those examples
29What is the output of the following code snippet?
int value = 25;
value = value * 2;
value --;
System.out.println(value);- A25
- B50
- C49
- D26
30What is the result of the following expression?
double d = 2.5 + 4 * -1.5 - (2.5 + 4) * -1.5;- A24.375
- B6.25
- C12.375
- D6
31What is the value of the following expression?
2 + 3 + 5 / 2- A5
- B6
- C7
- D7.5
32How many times will the following loop run?
int i = 0;
while (i < 10)
{
System.out.println(i);
i++;
}- A0
- B8
- C9
- D10
33What is the best way to improve the following code fragment?
if ((counter % 10) == 0
{
System.out.println(Counter is divisible by ten: " + counter);
counter++;
}
else
{
System.out.println("Counter is not divisible by ten: " + counter);
counter++:
}- AMove the duplicated code outside of the if statement
- BShorten the variable names
- CMove the brackets to save several lines of code
- DAdd semicolons after the if condition and the else reserved word
34What is the output of the following code fragment?
int i = 1;
int sum = 0;
while (i <= 11)
{
sum = sum + 1;
i++;
}
System.out.println("The value of sum is " + sum);- AThe value of sum is 65
- BThe value of sum is 66
- CThe value of sum is 55
- DThe value of sum is 56
35What does the method below return?
public static int findSomething (String str)
{
int position = 0;
while (position < str.length() && (str.charAt(position) != 'e'))
{
position++;
}
return position;
}- Athe position of the first 'e' in the string or the length of the string if there is no 'e'
- Bthe position of the last 'e' in the string or the length of the string if there is no 'e'
- Cthe position of the first character that is not an 'e' in the string or the length of the string if there is no character that is not an 'e'
- Dthe position of the last character that is not an 'e' in the string or the length of the string if there is no character that is not an 'e'
36Which of the following operators is used to invert a conditional statement?
- A!
- B!=
- C||
- D?
37Which of the following options checks that city is neither Chicago nor Dallas?
- Aif (!city.equals("Chicago") || !city.equals ("Dallas"))
- Bif (city.equals("Chicago") || city.equals ("Dallas"))
- Cif (!city.equals("Chicago") && !city.equals ("Dallas"))
- Dif (!city.equals("Chicago") || city.equals ("Dallas"))
38Assuming that a user enter 45, 78, and then 12, what is the output of the following code:
int num1 = 0;
int num2 = 0;
int num3 = 0;
Scanner in = new Scanner (System.in);
System.out.print("Enter a number: ");
num1 = in.nextInt();
System.out.print( "Enter a number: ");
num2 = in.nextInt();
System.out.print("Enter a number: ");
num3 = in.nextInt();
if (!(num1 > num2 && num1 > num3))
{
System.out.println(num1);
}
else if (! (num2 > num1 && num2 > num3))
{
System.out.println(num2);
}
else if (!(num3 > num1 && num3 > num2))
{
System.out.println(num3);
}- A12
- B45
- C78
- DThere is no output due to the compilation errors
39What is the first and last value of i to be displayed by the following code snippet?
int n = 20;
for (int i = 0; i<= n; i++)
{
for (int j = 0; j <= i; j++)
{
System.out.println("" + i);
}
}- A0 and 20
- B1 and 20
- C0 and 19
- D1 and 19
40Which of the following expressions represents a legal way of checking whether a value assigned to the number variable falls between 50 and 100 inclusive?
- Aif (number <= 100 && number <= 50)
- Bif (number >= 50 && number <= 100)
- Cif (number >= 50 || number <= 100)
- Dif (number >= 100 || number <= 50)
41How many times will the output line be printed in the following code snippet?
- A3 times
- B6 times
- C9 times
- D12 times
42What is the last output line of the following code snippet given below?
int i = 0;
while (I < 10)
{
for (int j = i; j >1; j--)
{
System.out.print(j + " ");
}
System.out.println("***");
i++;
}- A32 ***
- B9 8 7 6 5 4 3 2 ***
- C8 7 6 5 4 3 2 ***
- D2 ***
43Which of the following conditions tests for the user to enter the string "Hello?"
- Aif (s== "Hello")
- Bif (s.substring(0,5) == "Hello")
- Cif s.equals("Hello"))
- Dif (s = "Hello")
44What is the output of the following code snippet?
double income = 45000;
double cutoff = 55000;
double minIncome = 30000;
if (minIncome > income)
{
System.out.println("Minimum income requirement is not met.");
}
if (cutoff < income)
{
System.out.println("Maximum income limit is exceeded.");
}
else
{
System.out.println("Income requirement is met.");
}- AMinimum income requirement is not met
- BMaximum income limit is exceeded
- CIncome requirement is met
- DThere is no output
45What will be the output of the following code snippet?
int I;
int j;
for (i = 0; i < 7; i++)
{
for (j = 7; j > i; j--)
{
System.out.print("*");
}
System.out.println("");
}- AThe output will be a rectangle with six rows and seven columns of asterisks. The number of rows increments by one on completion of one iteration of the inner loop
- BThe output will be a right triangle with seven rows and seven columns of asterisks. The number of columns increments by one on completion of one iteration of the inner loop
- CThe output will be a rectangle with seven rows and seven columns of asterisks. The number of rows increments by one on completion of one iteration of the inner loop
- DThe output will be a right triangle with seven rows and seven columns of asterisks. The number of columns decrements by one on completion of one iteration of the inner loop
46In the following code snippet, when does the execution of the program switch from the inner loop to the outer loop?
int i;
int j;
for (i = 0; i <+ 9; i++)
{
for (j = 1; j < 5; j++)
{
System.out.print("Hello");
}
}- Awhen the value of j becomes 5
- Bwhen the program executes completely
- Cwhen the condition for the outer loop is met
- Dwhen the value of i is incremented
47What will be the range of the random numbers generated by the following code snippet?
Random generator = new Random ();
int r1 = generator.nextInt(50) + 1;- Abetween 1 and 49
- Bbetween 0 and 50
- Cbetween 0 and 49
- Dbetween 1 and 50
48Which of the following is the correct code snippet for throwing a pair of dice to get a sum of the numbers on two dice between 2 and 12 with different probabilities? (Assume Random generator = new Random();)
- Aint sum = generator.nextInt(6) + generator.nextInt(6) +2
- Bint sum = generator.nextInt(12) + 1
- Cint sum = generator.nextInt(6 +1) + generator.next(6+1)
- Dint sum = generator.nextInt(11) + 2
49Assuming that a valid price should be between 30 and 50, what does the following code snippet do?
final int MIN_PRICE = 30;
final int MAX_PRICE = 50;
int price = 0;
Scanner in = new Scanner (System.in);
System.out.println("Please enter the price: ");
price = in.nextInt();
if (price < MIN_PRICE)
{
System.out.println("Error: The price is too low.");
}
else if (price > MAX_PRICE)
{
System.out.println("Error: The price is too high.");
}
else
{
System.out.println("The price is in the valid price range.");
}- AThe code snippet ensures that the price value is between 30 and 50
- BThe code snippet only ensures that the price value is greater than 30
- CThe code snippet only ensures that the price value is less than 50
- DThe code snippet ensures that the price value is either less than 30 or greater than 50
50What output does this while loop generate?
j = 6;
while(j > 0)
{
System.out.print(j + ", ");
j++;
}- Ano output is generated
- B6, 5, 4, 3, 2, 1
- C6, 5, 4, 3, 2, 1,
- DThe output is infinite
Information
- Description
- Quiz on Untitled, created by Jamie M on 24/07/2018.
- Author
- Jamie M
- Language
- English
- Views
- 1
- Rating
- No ratings yet Rate this quiz