Quiz
Java Practice 2
Crie uma conta grátis para terminar este quiz
Salve seu progresso e veja sua nota final.
Já tem uma conta? Inicie sessão
Perguntas do quiz (200)
1To add a value 1 to variable x, you write (Choose all that apply.)
- Ax += 1;
- Bx = 1 + x;
- Cx := 1;
- Dx = x + 1;
- E1 + x = x;
2An overloaded method is one that
- Ahas a different name than another method, but the same parameters.
- Bhas the same name as another method, but different parameters (by number, types or order of the types).
- Chas the same name and parameters as a method defined in another class.
- Dhas the same name and parameters, but a different return type as another method.
3In Java, the word true is ________.
- Asame as value 0
- Bsame as value 1
- Ca Boolean literal
- Da Java keyword
4The ________ method parses a string s to a double value.
- Adouble.parseDouble(s);
- Bdouble.parse(s);
- CDouble.parsedouble(s);
- DDouble.parseDouble(s);
5Suppose variable gender is MALE and age equals 60, how is the expression
( gender == FEMALE ) && ( age >= 65 )
evaluated?- AThe condition ( gender == FEMALE ) is evaluated first and the evaluation stops immediately.
- BThe condition ( age >= 65 ) is evaluated first and the evaluation stops immediately.
- CBoth conditions are evaluated, from left to right.
- DBoth conditions are evaluated, from right to left.
6Will the following program terminate?
int balance = 10;
while (true) {
if (balance < 9) continue;
balance = balance - 9;
}- AYes
- BNo
7What is the output of the following code?
boolean even = false;
System.out.println((even ? "true" : "false"));- Atrue
- Btrue false
- Cfalse
- Dnothing
8Which command compiles the Java source code file Welcome.java?
- Acd Welcome.java
- Bjavac Welcome.java
- Cjava Welcome.java
- Dcompile Welcome.java
9The following code fragment reads in two numbers: (Choose all that apply.)
Scanner input = new Scanner(System.in);
int i = input.nextInt();
double d = input.nextDouble();
What are the correct ways to enter these two numbers?- AEnter an integer, an Enter key, a double value, and then the Enter key.
- BEnter an integer, a space, a double value, and then the Enter key.
- CEnter an integer, two spaces, a double value, and then the Enter key.
- DEnter a numeric value with a decimal point, a space, an integer, and then the Enter key.
10Suppose s1 and s2 are two strings. Which of the following statements or expressions is incorrect? (Choose all that apply.)
- Aboolean b = s1.compareTo(s2);
- Bchar c = s1[0];
- Cchar c = s1.charAt(s1.length());
- DString s3 = s1 - s2;
11Which statement is true?
- AA while statement cannot be nested inside another while statement.
- BAn if statement cannot be nested inside another if statement.
- CA while statement cannot be nested inside an if statement.
- DNone of the above is true.
12What is output by the following Java code segment?
int temp;
temp = 180;
while ( temp != 80 )
{
if ( temp > 90 )
{
System.out.print( "This porridge is too hot! " );
// cool down
temp = temp – ( temp > 150 ? 100 : 20 );
} // end if
else
{
if ( temp < 70 )
{
System.out.print(
"This porridge is too cold! ");
// warm up
temp = temp + (temp < 50 ? 30 : 20);
} // end if
} // end else
} // end while
if ( temp == 80 )
System.out.println( "This porridge is just right!" );- AThis porridge is too cold! This porridge is just right!
- BThis porridge is too hot! This porridge is just right!
- CThis porridge is just right!
- DNone of the above.
13Suppose s is a string with the value "java". What will be assigned to x if you execute the following code?
char x = s.charAt(4);- A'a'
- B'v'
- CNothing will be assigned to x, because the execution causes the runtime error StringIndexOutofBoundsException.
14Which of these statements best defines scope?
- AScope refers to the classes that have access to a variable.
- BScope determines whether a variable's value can be altered.
- CScoping allows the programmer to use a class without using its fully qualified name.
- DScope is the portion of a program that can refer to an entity by its simple name.
15What is the value of (double)5/2?
- A2.5
- B2
- C3
- D2.0
- E3.0
16What does the expression x %= 10 do?
- AAdds 10 to the value of x, and stores the result in x.
- BDivides x by 10 and stores the remainder in x.
- CDivides x by 10 and stores the integer result in x.
- DNone of the above.
17Which of the following is not a package in the Java API?
- Ajava.characters.
- Bjava.awt.
- Cjavax.swing.event.
- Djava.lang.
18Suppose x=10 and y=10 what is x after evaluating the expression (y >= 10) || (x-- > 10)?
- A11
- B9
- C10
19Each time a method is invoked, the system stores parameters and local variables in an area of memory, known as ________, which stores elements in last-in first-out fashion.
- Aa stack
- Ban array
- Ca heap
- Dstorage area
20Counter-controlled repetition requires
- AA control variable and initial value.
- BA control variable increment (or decrement).
- CA condition that tests for the final value of the control variable.
- DAll of the above.
21Which of the following assignment statements is correct? (Choose all that apply.)
- Achar c = 'd';
- Bchar c = "100";
- Cchar c = "d";
- Dchar c = 100;
22To add 0.01 + 0.02 + ... + 1.00, what order should you use to add the numbers to get better accuracy?
- Aadd 1.00, 0.99, 0.98, ..., 0.02, 0.01 in this order to a sum variable whose initial value is 0.
- Badd 0.01, 0.02, ..., 1.00 in this order to a sum variable whose initial value is 0.
23(int)(Math.random() * (65535 + 1)) returns a random number ________.
- Abetween 1 and 65535
- Bbetween 0 and 65536
- Cbetween 0 and 65535
- Dbetween 1 and 65536
24What is the return value of "SELECT".substring(4, 4)?
- AT
- BC
- Can empty string
- DE
25What is the value of (double)(5/2)?
- A2
- B2.0
- C3
- D2.5
- E3.0
26Which of the following statements will print a single line containing "hello there"?
- ASystem.out.println( "hello" );
System.out.println( " there" ); - BSystem.out.println( "hello" , " there" );
- CSystem.out.println( "hello" );
System.out.print( " there" ); - DSystem.out.print( "hello" );
System.out.println( " there" );
- ASystem.out.println( "hello" );
27Which of the following operators are right-associative?
- A*
- B%
- C&&
- D+
- E=
28What is displayed on the console when running the following program?
class Test {
public static void main (String[ ] args) {
try {
System.out.println("Welcome to Java");
}
finally {
System.out.println("The finally clause is executed");
}
}
}- AThe finally clause is executed
- BWelcome to Java followed by The finally clause is executed in the next line
- CNone of the above
- DWelcome to Java
29Which of the following exceptions is a checked exception?
- AArithmeticException.
- BIOException.
- CInputMismatchException.
- DRuntimeException.
30What exception type does the following program throw?
public class Test {
public static void main(String[ ] args) {
System.out.println(1 / 0);
}
}- AClassCastException
- BNo exception
- CArithmeticException
- DStringIndexOutOfBoundsException
- EArrayIndexOutOfBoundsException
31What is wrong in the following program?
class Test {
public static void main (String[ ] args) {
try {
System.out.println("Welcome to Java");
}
}
}- AYou cannot have a try block without a catch block.
- BA method call that does not declare exceptions cannot be placed inside a try block.
- CYou cannot have a try block without a catch block or a finally block.
- DNothing is wrong.
32Which of the following statements is true?
- AThe new exception class should extend RuntimeException if the program should be required to handle the exception.
- BUsing existing exceptions makes the program less robust.
- CAlways create your own exception classes.
- DLike any other class, an exception class can contain fields and methods.
33What is displayed on the console when running the following program?
class Test {
public static void main(String[ ] args) {
try {
System.out.println("Welcome to Java");
int i = 0;
int y = 2/i;
System.out.println("Welcome to HTML");
}
finally {
System.out.println("The finally clause is executed");
}
}
}- AThe program displays three lines: Welcome to Java, Welcome to HTML, The finally clause is executed.
- BWelcome to Java.
- CNone of the above.
- DWelcome to Java followed by The finally clause is executed in the next line.
34What is the difference between a try block and a try statement?
- AA try statement refers to the block of code following the keyword try, while the try block refers to the try keyword and the block of code following this keyword.
- BThe try statement refers to the keyword try followed by a block of code. The try statement and its corresponding catch and/or finally clauses together form a try block.
- CThere is no difference; the terms can be used interchangeably.
- DThe try block refers to the keyword try followed by a block of code. The try block and its corresponding catch and/or finally clauses together form a try statement.
35If the catch-or-declare requirement for a checked exception is not satisfied:
- Aa stack trace will be displayed, along with a message indicating that the exception must be caught.
- Ba stack trace will be displayed indicating the exception that has occurred and where it occurred.
- Cthe compiler will issue an error message indicating that the exception must be caught or declared.
- Dthe compiler will issue an error message indicating that the exception must be caught.
36When an exception occurs it is said to have been:
- Ahandled.
- Bthrown.
- Ccaught.
- Ddeclared.
37Exceptions can be thrown by:
- AAll of the above.
- Bcode in a try block.
- Cthe Java Virtual Machine.
- Dcalls from a try block to other methods.
38Analyze the following code:
class Test {
public static void main(String[ ] args)
throws MyException {
System.out.println("Welcome to Java");
}
}
class MyException extends Error {
}- AYou cannot declare an exception in the main method.
- BYou declared an exception in the main method, but you did not throw it.
- CThe program has a compilation error.
- DYou should not declare a class that extends Error, because Error raises a fatal error that terminates the program.
39The following code causes Java to throw ________.
int number = Integer.MAX_VALUE + 1;- AError
- BRuntimeException
- Cno exceptions
- DException
- EThrowable
40A Java exception is an instance of ________.
- ARuntimeException
- BError
- CNumberFormatException
- DException
- EThrowable
41Which of the following statements is true?
- AThe code in a finally block is executed only if an exception occurs.
- BThe code in a finally block is executed only if an exception does not occur.
- CNone of the above are true.
- DThe code in a finally block is executed only if there are no catch blocks.
42Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause runtime errors? (Choose all that apply.)
- Ax.get(1)
- Bx.size()
- Cx.remove(2)
- Dx.get(2)
- Ex.set(2, "New York");
43Which keyword is used to specify that a class will define the methods of an interface?
- Aimplements.
- Bextends.
- Cdefines.
- Duses.
44A(n) ____________________ class cannot be instantiated.
- Apolymorphic.
- Babstract.
- Cconcrete.
- Dfinal.
45Failure to prefix the superclass method name with the keyword super and a dot (.) separator when referencing the superclass's method causes ________.
- Aa syntax error.
- Ba compile-time error.
- Ca runtime error.
- Dinfinite recursion.
46The equals method is defined in the Object class. Which of the following is correct to override it in the String class?
- Apublic static boolean equals(String other)
- Bpublic boolean equals(String other)
- Cpublic static boolean equals(Object other)
- Dpublic boolean equals(Object other)
47Inheritance means ________.
- Athat a variable of supertype can refer to a subtype object
- Bthat data fields should be declared private
- Cthat a class can contain another class
- Dthat a class can extend another class
48Analyze the following code:
Cylinder cy = new Cylinder(1, 1);
Circle c = cy;- AThe code has a runtime error.
- BThe code has a compile error.
- CThe code is fine.
49Encapsulation means ________.
- Athat a class can extend another class
- Bthat data fields should be declared private
- Cthat a variable of supertype can refer to a subtype object
- Dthat a class can contain another class
50private fields of a superclass can be accessed in a subclass
- Aby calling public or protected methods declared in the superclass.
- BAll of the above.
- Cdirectly.
- Dby calling private methods declared in the superclass.
51Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause the list to become [Beijing]? (Choose all that apply.)
- Ax.remove(1)
- Bx.remove("Singapore")
- Cx.remove(0)
- Dx.remove(2)
52You can assign ________ to a variable of Object[ ] type. (Choose all that apply.)
- Anew java.util.Date[100]
- Bnew String[100]
- Cnew double[100]
- Dnew char[100]
- Enew int[100]
53Which of the following statements are true? (Choose all that apply.)
- AA private method cannot be overridden. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated.
- BOverloading a method is to provide more than one method with the same name but with different signatures to distinguish them.
- CIt is a compilation error if two methods differ only in return type in the same class.
- DTo override a method, the method must be defined in the subclass using the same signature and compatible return type as in its superclass.
- EA static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden.
54Given the following code:
class C1 {}
class C2 extends C1 { }
class C3 extends C2 { }
class C4 extends C1 {}
C1 c1 = new C1();
C2 c2 = new C2();
C3 c3 = new C3();
C4 c4 = new C4();
Which of the following expressions evaluates to false?- Ac4 instanceof C2
- Bc1 instanceof C1
- Cc3 instanceof C1
- Dc2 instanceof C1
55Object-oriented programming allows you to derive new classes from existing classes. This is called ________.
- Ageneralization
- Bencapsulation
- Cinheritance
- Dabstraction
56What is the printout for the third statement in the main method?
public class Foo {
static int i = 0;
static int j = 0;
public static void main(String[ ] args) {
int i = 2;
int k = 3;
{
int j = 3;
System.out.println("i + j is " + i + j);
}
k = i + j;
System.out.println("k is " + k);
System.out.println("j is " + j);
}
}- Aj is 3
- Bj is 1
- Cj is 2
- Dj is 0
57The UML represents operations by listing the operation name, followed by a ________-separated list of parameters in parentheses, a ________ and the return type.
- Acomma, colon.
- Bcolon, semicolon.
- Ccolon, colon.
- Dcomma, semicolon.
58Analyze the following code:
class Circle {
private double radius;
public Circle(double radius) {
radius = radius;
}
}- AThe program will compile, but you cannot create an object of Circle with a specified radius. The object will always have radius 0.
- BThe program has a compilation error because you cannot assign radius to radius.
- CThe program does not compile because Circle does not have a default constructor.
- DThe program has a compilation error because it does not have a main method.
59Which diagram models system structure?
- AState machine diagram.
- BActivity diagram.
- CClass diagram.
- DSequence diagram.
60Which of the following is the first stage of the software life cycle?
- Atesting.
- Bimplementation.
- Crequirements gathering.
- Ddesign.
61Analyze the following code.
public class Test {
int x;
public Test(String t) {
System.out.println("Test");
}
public static void main(String[ ] args) {
Test test = new Test();
System.out.println(test.x);
}
}- AThe program has a compile error because System.out.println method cannot be invoked from the constructor.
- BThe program has a compile error because Test does not have a default constructor.
- CThe program has a compile error because you cannot create an object from the class that defines the object.
- DThe program has a compile error because x has not been initialized.
62Analyze the following code.
public class Test {
int x;
public Test(String t) {
System.out.println("Test");
}
public static void main(String[ ] args) {
Test test = null;
System.out.println(test.x);
}
}- AThe program has a compile error because you cannot create an object from the class that defines the object.
- BThe program has a compile error because x has not been initialized.
- CThe program has a compile error because test is not initialized.
- DThe program has a runtime NullPointerException because test is null while executing test.x.
- EThe program has a compile error because Test does not have a default constructor.
63Assume java.util.Date[ ] dates = new java.util.Date[10], which of the following statements are true? (Choose all that apply.)
- Adates is null.
- Bdates = new Date() is fine, which creates a new Date object and assigns to dates.
- Cdates = new java.util.Date[5] is fine, which assigns a new array to dates.
- Ddates[0] is null.
64The java.util.Date class is introduced in this section. Which of the following code creates an object of the Date class? (Choose all that apply.)
A:
public class Test {
public Test() {
new java.util.Date();
}
}
B:
public class Test {
public Test() {
java.util.Date date = new java.util.Date();
}
}- AB
- BA
- Cneither
65________ is a construct that defines objects of the same type.
- AA class
- BA method
- CA data field
- DAn object
66Which two Java primitive types store floating-point numbers?
- Adecimal and float.
- Bpoint and double.
- Cdecimal and point.
- Dfloat and double.
67What is the printout of the second println statement in the main method?
public class Foo {
int i;
static int s;
public static void main(String[ ] args) {
Foo f1 = new Foo();
System.out.println("f1.i is " + f1.i + " f1.s is " + f1.s);
Foo f2 = new Foo();
System.out.println("f2.i is " + f2.i + " f2.s is " + f2.s);
Foo f3 = new Foo();
System.out.println("f3.i is " + f3.i + " f3.s is " + f3.s);
}
public Foo() {
i++;
s++;
}
}- Af2.i is 2 f2.s is 2
- Bf2.i is 1 f2.s is 2
- Cf2.i is 2 f2.s is 1
- Df2.i is 1 f2.s is 1
68Which of the following statements are true? (Choose all that apply.)
- AConstructors are invoked using the new operator when an object is created.
- BAt least one constructor must always be defined explicitly.
- CConstructors do not have a return type, not even void.
- DA default no-arg constructor is provided automatically if no constructors are explicitly declared in the class.
- EConstructors must have the same name as the class itself.
69An object is an instance of a ________.
- Amethod
- Bprogram
- Cdata
- Dclass
70When you pass an array to a method, the method receives ________.
- Athe length of the array
- Bthe reference of the array
- Ca copy of the first element
- Da copy of the array
71Assume int[ ] t = {1, 2, 3, 4}. What is t.length?
- A4
- B0
- C5
- D3
72Analyze the following code:
public class Test {
public static void main(String[ ] args) {
int[ ] x = {1, 2, 3, 4};
int[ ] y = x;
x = new int[2];
for (int i = 0; i < x.length; i++)
System.out.print(x[i] + " ");
}
}- AThe program displays 0 0 3 4
- BThe program displays 0 0
- CThe program displays 0 0 0 0
- DThe program displays 1 2 3 4
73Analyze the following code.
int[ ] list = new int[5];
list = new int[6];- AThe code has runtime errors because the variable list cannot be changed once it is assigned.
- BThe code has compile errors because you cannot assign a different size array to list.
- CThe code has compile errors because the variable list cannot be changed once it is assigned.
- DThe code can compile and run fine. The second line assigns a new array to list.
74In the following code, what is the printout for list1?
class Test {
public static void main(String[ ] args) {
int[ ] list1 = {1, 2, 3};
int[ ] list2 = {1, 2, 3};
list2 = list1;
list1[0] = 0; list1[1] = 1; list2[2] = 2;
for (int i = 0; i < list1.length; i++)
System.out.print(list1[i] + " ");
}
}- A0 1 2
- B1 2 3
- C1 1 1
- D0 1 3
75How many elements are in array double[ ] list = new double[5]?
- A0
- B4
- C5
- D6
76The selectionSort method is defined in this section. Assume list is {3.1, 3.1, 2.5, 6.4, 2.1}, what is the content of list after the first iteration of the outer loop in the method?
- A2.1, 2.5, 3.1, 3.1, 6.4
- B2.5, 3.1, 3.1, 6.4, 2.1
- C3.1, 3.1, 2.5, 2.1, 6.4
- D3.1, 3.1, 2.5, 6.4, 2.1
77Analyze the following code:
class Test {
public static void main(String[ ] args) {
System.out.println(xmethod(5));
}
public static int xmethod(int n, long t) {
System.out.println("int");
return n;
}
public static long xmethod(long n) {
System.out.println("long");
return n;
}
}- AThe program displays long followed by 5.
- BThe program displays int followed by 5.
- CThe program runs fine but displays things other than 5.
- DThe program does not compile because the compiler cannot distinguish which xmethod to invoke.
78Which statement is false?
- ARedeclaring a method parameter as a local variable in the method's body is a compilation error.
- BPlacing a semicolon after the right parenthesis enclosing the parameter list of a method declaration is a syntax error.
- CForgetting to return a value from a method that should return a value is a compilation error.
- DIf a method does not return a value, the return-value-type in the method declaration can be omitted.
79What is Math.ceil(3.6)?
- A5.0
- B3.0
- C4.0
- D3
80Programs designed for maintainability are constructed from small simple pieces or modules. Modules in Java are called:
- Aboth methods and classes.
- Barguments.
- Cclasses.
- Dmethods.
81Which of the following promotions of primitive types is not allowed to occur?
- Achar to int.
- Bint to double.
- Cshort to long.
- Ddouble to float.
82What is Math.rint(3.6)?
- A4.0
- B3.0
- C5.0
- D3
83Method log takes the logarithm of its argument with respect to what base?
- Api
- B2
- Ce
- D10
84Does the return statement in the following method cause compile errors?
public static void main(String[ ] args) {
int max = 0;
if (max != 0)
System.out.println(max);
else
return;
}- AYes
- BNo
85Analyze the following code.
public class Test {
public static void main(String[ ] args) {
System.out.println(m(2));
}
public static int m(int num) {
return num;
}
public static void m(int num) {
System.out.println(num);
}
}- AThe program runs and prints 2 once.
- BThe program has a compile error because the second m method is defined, but not invoked in the main method.
- CThe program has a compile error because the two methods m have the same signature.
- DThe program runs and prints 2 twice.
86Arguments to methods always appear within ________.
- Abrackets
- Bquotation marks
- Ccurly braces
- Dparentheses
87Math static method random generates a random double value in the range from 0.0
- Aup to and including 1.0
- Bup to but not including 1.0
- Cup to and including 100.0
- Dup to but not including 100.0
88Which of the following primitive types is never promoted to another type?
- Adouble.
- Bbyte.
- Cboolean.
- Dboth double and boolean
89Suppose your method does not return any value, which of the following keywords can be used as a return type?
- Avoid
- Bpublic
- Cdouble
- Dint
- Enone of the above
90A variable defined inside a method is referred to as ________.
- Aa block variable
- Ba global variable
- Ca local variable
- Da method variable
91The client can use a method without knowing how it is implemented. The details of the implementation are encapsulated in the method and hidden from the client who invokes the method. This is known as ________. (Choose all that apply.)
- Amethod hiding
- Binformation hiding
- Csimplifying method
- Dencapsulation
92Which of the following statements about the break statement is false?
- ACommon uses of the break statement are to escape early from a loop or to skip the remainder of a switch.
- BA break statement can only break out of an immediately enclosing while, for, do…while or switch statement.
- CThe break statement is used to exit a repetition structure early and continue execution after the loop.
- DThe break statement, when executed in a while, for or do…while, skips the remaining statements in the loop body and proceeds with the next iteration of the loop.
93Consider the classes below:
public class TestA
{
public static void main( String args[] )
{
int x = 2, y = 20, counter = 0;
for ( int j = y % x; j < 100; j += ( y / x ) )
counter++;
} // end main
} // end class TestA
public class TestB
{
public static void main(String args[])
{
int counter = 0;
for ( int j = 10; j > 0; --j )
++counter;
} // end main
} // end class TestB
Which of the following statements is true?- AThe value of counter will be different at the end of each for loop for each class.
- BThe value of j will be the same for each loop for all iterations
- CBoth (a) and (b) are true.
- DNeither (a) nor (b) is true.
94For the two code segments below:
Segment A
int q = 5;
switch( q )
{
case 1:
System.out.println( 1 );
case 2:
System.out.println( 2 );
case 3:
System.out.println( 3 );
case 4:
System.out.println( 4 );
case 5:
System.out.println( 5 );
default:
System.out.println( "default" );
} // end switch
Segment B
q = 4;
switch( q )
{
case 1:
System.out.println( 1 );
case 2:
System.out.println( 2 );
case 3:
System.out.println( 3 );
case 4:
System.out.println( 4 );
case 5:
System.out.println( 5 );
default:
System.out.println( "default" );
} // end switch
Which of the following statements is true?- AThe output for Segment B is: ?45default
- BThe output for Segment B is: ?4
- CThe output for Segment A is: ?default
- DThe output for Segment A is: ?5?default
95What is sum after the following loop terminates?
int sum = 0;
int item = 0;
do {
item++;
sum += item;
if (sum > 4) break;
}
while (item < 5);- A7
- B8
- C5
- D6
96The control variable of a counter-controlled loop should be declared as ________to prevent errors.
- Adouble.
- BAny of the above.
- Cint.
- Dfloat.
97Which of the following statements about a do..while repetition statement is true?
- AThe body of a do..while loop is executed only if the terminating condition is true.
- BThe body of a do..while loop is executed only once.
- CThe body of a do..while loop is always executed at least once.
- DNone of the above
98Which expression is equivalent to if ( ! ( grade == sentinelValue ) )?
- A! if ( grade == sentinelValue ).
- B! if ( grade !== sentinelValue ).
- Cif ( grade !== sentinelValue ).
- Dif ( grade != sentinelValue ).
99Which of the following assignment statements is correct to assign character 5 to c?
- Achar c = '5';
- Bchar c = 5;
- Cchar c = "5";
- Dchar c = "344";
100Analyze the following code:
int i = 3434; double d = 3434;
System.out.printf("%5.1f %5.1f", i, d);- AThe code compiles and runs fine to display 3434.0 3434.0.
- BThe code compiles and runs fine to display 3434 3434.0.
- Ci is an integer, but the format specifier %5.1f specifies a format for double value. The code has an error.
101What is "Welcome" + 1 + 1 * 2?
- AWelcome11*2
- BWelcome4
- CWelcome12
- DWelcome3
102Note that the Unicode for character A is 65. The expression 'A' + 1 evaluates to ________.
- A66
- BB
- CA1
- DIllegal expression
103________ returns true.
- A"peter".compareToIgnoreCase("Peter")
- B"peter".compareToIgnoreCase("peter")
- C"peter".equalsIgnoreCase("Peter")
- D"peter".equalsIgnoreCase("peter")
- E"peter".equals("peter")
104To concatenate s1, s2, s3, and s4, you write ________.
- As1.concate(s2).concate(s3).concate(s4);
- Bs1.concate(s2) + s3.concate(s4);
- C((s1.concate(s2)).concate(s3)).concate(s4);
- Ds1.concate(s2).(concate(s3).concate(s4));
105Assume that the ASCII code for character c is 99. What is the printout of the following code?
System.out.println("a" + 'c');- Aa99
- Bac
- C9799
- D196
106Math.sin(Math.PI) returns ________.
- A0.0
- B1.0
- C0.5
- D0.4
107parseDouble is a method in the ________ class.
- AInteger
- BDouble
- CMath
- DSystem
108Is 'a' larger than 'A'?
- AYes
- BNo
109The order of the precedence (from high to low) of the operators +, *, &&, ||, & is:
- A*, +, &, &&, ||
- B*, +, &, ||, &&
- C*, +, &&, ||, &
- D&, ||, &&, *, +
- E&&, ||, &, *, +
110Analyze the following code:
if (x < 100) && (x > 10)
System.out.println("x is between 10 and 100");- AThe statement has compile errors because (x<100) & (x > 10) must be enclosed inside parentheses.
- BThe statement has compile errors because (x<100) & (x > 10) must be enclosed inside parentheses and the println(…) statement must be put inside a block.
- CThe statement compiles fine.
- DThe statement compiles fine, but has a runtime error.
111The following code displays ________.
double temperature = 50;
if (temperature >= 100)
System.out.println("too hot");
else if (temperature <= 40)
System.out.println("too cold");
else
System.out.println("just right");- Atoo hot
- Btoo hot too cold just right
- Cjust right
- Dtoo cold
112To check whether a char variable ch is an uppercase letter, you write ________.
- A('A' <= ch <= 'Z')
- B(ch >= 'A' && ch <= 'Z')
- C(ch >= 'A' || ch <= 'Z')
- D(ch >= 'A' && ch >= 'Z')
113The statement System.out.printf("%10s", 123456) outputs ________. (Note: * represents a space)
- A****123456
- B23456*****
- C12345*****
- D123456****
114The equal comparison operator in Java is ________.
- A<>
- B==
- C!=
- D^=
115To improve readability and maintainability, you should declare ________ instead of using literal values such as 3.14159.
- Aclasses
- Bvariables
- Cmethods
- Dconstants
116To assign a value 1 to variable x, you write
- A1 = x;
- Bx = 1;
- Cx := 1;
- Dx == 1;
- E1 := x;
117The ________ method parses a string s to an int value.
- Ainteger.parseInteger(s);
- BInteger.parseInt(s);
- Cinteger.parseInt(s);
- DInteger.parseInteger(s);
118Will System.out.println((char)4) display 4?
- ANo
- BYes
119What is the printout of the following code:
double x = 5.5;
int y = (int)x;
System.out.println("x is " + x + " and y is " + y);- Ax is 5 and y is 6
- Bx is 5.5 and y is 5
- Cx is 6 and y is 6
- Dx is 5.5 and y is 5.0
- Ex is 6.0 and y is 6.0
120What is y displayed in the following code?
public class Test1 {
public static void main(String[ ] args) {
int x = 1;
int y = x = x + 1;
System.out.println("y is " + y);
}
}- Ay is 0.
- BThe program has a compile error since x is redeclared in the statement int y = x = x + 1.
- Cy is 2 because x + 1 is assigned to x and then x is assigned to y.
- Dy is 1 because x is assigned to y first.
12124 % 5 is ________.
- A1
- B3
- C2
- D0
- E4
122Which of the following statements is correct to display Welcome to Java on the console? (Choose all that apply.)
- ASystem.out.println("Welcome to Java");
- BSystem.println('Welcome to Java');
- CSystem.out.print('Welcome to Java');
- DSystem.out.println('Welcome to Java');
- ESystem.out.print("Welcome to Java");
123Why do computers use zeros and ones?
- Abecause combinations of zeros and ones can represent any numbers and characters.
- Bbecause binary numbers are the bases upon which all other number systems are built.
- Cbecause binary numbers are simplest.
- Dbecause digital devices have two stable states and it is natural to use one state for 0 and the other for 1.
124Every statement in Java ends with ________.
- Aa semicolon (;)
- Ba period (.)
- Can asterisk (*)
- Da comma (,)
125________ are instructions to the computer. (Choose all that apply.)
- APrograms
- BSoftware
- CKeyboards
- DHardware
126________ is the physical aspect of the computer that can be seen.
- ASoftware
- BApplication program
- COperating system
- DHardware
127The extension name of a Java source code file is
- A.class
- B.obj
- C.java
- D.exe
128________ is a program that runs on a computer to manage and control a computer's activities.
- AModem
- BJava
- COperating system
- DCompiler
- EInterpreter
129________ translates high-level language program into machine language program.
- AA compiler
- BThe operating system
- CCPU
- DAn assembler
130Which of the following are storage devices? (Choose all that apply.)
- Ahard disk
- Bfloppy disk
- CCD-ROM
- Dflash stick
131The ________ method displays a message dialog box. (Choose all that apply.)
- AJOptionPane.showMessage(null, "Welcome to Java!", "Example 1.2 Output", JOptionPane.INFORMATION_MESSAGE);
- BJOptionPane.displayMessageDialog(null, "Welcome to Java!", "Example 1.2 Output", JOptionPane.INFORMATION_MESSAGE);
- CJOptionPane.showMessageDialog(null, "Welcome to Java!", "Example 1.2 Output", JOptionPane.INFORMATION_MESSAGE);
- DJOptionPane.showMessageDialog(null, "Welcome to Java!");
- EJOptionPane.displayMessage(null, "Welcome to Java!", "Example 1.2 Output", JOptionPane.INFORMATION_MESSAGE);
132What is y displayed in the following code?
public class Test {
public static void main(String[ ] args) {
int x = 1;
int y = x++ + x;
System.out.println("y is " + y);
}
}- Ay is 4.
- By is 1.
- Cy is 3.
- Dy is 2.
133Which of the following is a constant, according to Java naming conventions? (Choose all that apply.)
- AReadInt
- BTest
- CMAX_VALUE
- Dread
- ECOUNT
134Suppose a Scanner object is created as follows:
Scanner input = new Scanner(System.in);
What method do you use to read an int value?- Ainput.nextInt();
- Binput.int();
- Cinput.nextInteger();
- Dinput.integer();
135The System.currentTimeMillis() returns ________.
- Athe current time
- Bthe current time in milliseconds since midnight, January 1, 1970
- Cthe current time in milliseconds since midnight, January 1, 1970 GMT (the Unix time)
- Dthe current time in milliseconds
- Ethe current time in milliseconds since midnight
136The Unicode of 'a' is 97. What is the Unicode for 'c'?
- A96
- B97
- C99
- D98
137________ is the Java assignment operator.
- A:=
- B==
- C=:
- D=
138The statement System.out.printf("%5d", 123456) outputs ________.
- A12345.6
- B23456
- C123456
- D12345
139Which of the Boolean expressions below is incorrect? (Choose all that apply.)
- A(-10 < x < 0)
- B(true) && (3 => 4)
- C(x != 0) || (x = 0)
- D(x > 0) || (x < 0)
- E!(x > 0) && (x > 0)
140"ab".compareTo("aB") is ________.
- Agreater than 0
- Bless than 0
- Cequal to 0
- Dless than or equal to 0
141"smiles".substring(1, 5) returns "mile".
- Atrue
- Bfalse
142Which of the following is a possible output for (int)(51 * Math.random())?
- A0
- B50
- C100
- D500
143Which of the following statement prints smith\exam1\test.txt?
- ASystem.out.println("smith\exam1\test.txt");
- BSystem.out.println("smith\\exam1\\test.txt");
- CSystem.out.println("smith\"exam1\"test.txt");
- DSystem.out.println("smith"\exam1"\test.txt");
144Math.sqrt(4) returns ________.
- A2
- B2.0
- C1
- D1.0
145'3' - '2' + 'm' / 'n' is ________.
- A0
- B1
- C2
- D3
146An int variable can hold ________.
- A'x'
- B120
- C120.0
147Analyze the following fragment:
double sum = 0;
double d = 0;
while (d != 10.0) {
d += 0.1;
sum += sum + d;
}- AAfter the loop, sum is 0 + 0.1 + 0.2 + 0.3 + ... + 1.9
- BThe program does not compile because sum and d are declared double, but assigned with integer value 0.
- CThe program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.
- DThe program never stops because d is always 0.1 inside the loop.
148After the continue outer statement is executed in the following loop, which statement is executed?
outer:
for (int i = 1; i < 10; i++) {
inner:
for (int j = 1; j < 10; j++) {
if (i * j > 50)
continue outer;
System.out.println(i * j);
}
}
next:- AThe control is in the outer loop, and the next iteration of the outer loop is executed.
- BThe program terminates.
- CThe statement labeled next.
- DThe control is in the inner loop, and the next iteration of the inner loop is executed.
149Consider the following two Java code segments:
Segment 1
int i = 0;
for ( int i = 0; i <= 20; i++ ) {
System.out.println( i );
}
Segment 2
while ( i < 20 )
{
System.out.println( i );
i++;
}
Which of the following statements are true?- AThe output from these segments is not the same.
- BThe scope of the control variable i is different for the two segments.
- CThey are both true
- DNeither are true
150Which statement prints the floating-point value 123.456 right justified with a field width of 10?
- ASystem.out.printf( “%10.3f”, 123.456 );
- BSystem.out.printf( “%f10.3”, 123.456 );
- CSystem.out.printf( “%d10.3”, 123.456 );
- DSystem.out.printf( “%10.3d”, 123.456 );
151Which statement below is not true?
- AStructured programming produces programs that are easier to modify
- BStructured programming produces programs that are easier to test.
- CStructured programming promotes simplicity.
- DStructured programming requires four forms of control.
152An enumeration is a special class that is introduced by the keyword ________ and a type name.
- Aclass.
- BclassEnum.
- Cenumeration.
- Denum.
153When an object is concatenated with a String:
- Aa compilation error occurs.
- Bthe object's class name is used.
- Cthe object's toString method is implicitly called to obtain the String representation of the object.
- Da runtime error occurs.
154Suppose method1 is declared as
void method1 ( int a, float b )
Which of the following methods correctly overloads method1?- Avoid method2 ( int a, float b ).
- Bvoid method2 ( float a, int b ).
- Cvoid method1 ( float a, int b ).
- Dvoid method1 ( int b, float a ).
155Which operator can be used in string concatenation?
- A++.
- B+=.
- C=+.
- D*.
156(char)('a' + Math.random() * ('z' - 'a' + 1)) returns a random character ________.
- Abetween 'a' and 'z'
- Bbetween 'b' and 'z'
- Cbetween 'a' and 'y'
- Dbetween 'b' and 'y'
157Which of the following is the best for generating random integer 0 or 1?
- A(int)(Math.random() + 0.2)
- B(int)Math.random()
- C(int)Math.random() + 1
- D(int)(Math.random() + 0.8)
- E(int)(Math.random() + 0.5)
158Information is passed to a method in:
- Athe method body.
- Bthe method name.
- Cthe arguments to the method.
- Dthat method's return.
159The signature of a method consists of ________.
- Amethod name
- Bmethod name and parameter list
- Creturn type, method name, and parameter list
- Dparameter list
160Analyze the following code:
public class Test {
public static void main(String[ ] args) {
int[ ] a = new int[4];
a[1] = 1;
a = new int[2];
System.out.println("a[1] is " + a[1]);
}
}- AThe program displays a[1] is 1.
- BThe program has a runtime error because a[1] is not initialized.
- CThe program displays a[1] is 0.
- DThe program has a compile error because new int[2] is assigned to a.
161Which of the following declarations are correct? (Choose all that apply.)
- Apublic static void print(double... numbers)
- Bpublic static double... print(double d1, double d2)
- Cpublic static void print(double... numbers, String name)
- Dpublic static void print(String... strings, double... numbers)
- Epublic static void print(int n, double... numbers)
162The reverse method is defined in this section. What is list1 after executing the following statements?
int[ ] list1 = {1, 2, 3, 4, 5, 6};
int[ ] list2 = reverse(list1);- Alist1 is 6 6 6 6 6 6
- Blist1 is 0 0 0 0 0 0
- Clist1 is 1 2 3 4 5 6
- Dlist1 is 6 5 4 3 2 1
163Analyze the following code:
public class Test {
public static void main(String[ ] args) {
int[ ] x = {1, 2, 3, 4};
int[ ] y = x;
x = new int[2];
for (int i = 0; i < y.length; i++)
System.out.print(y[i] + " ");
}
}- AThe program displays 1 2 3 4
- BThe program displays 0 0 0 0
- CThe program displays 0 0
- DThe program displays 0 0 3 4
164Suppose int i = 5, which of the following can be used as an index for array double[ ] t = new double[100]? (Choose all that apply.)
- A(int)(Math.random() * 100))
- BMath.random() * 100
- Ci
- Di + 6.5
- Ei + 10
165If you declare an array double[ ] list = {3.4, 2.0, 3.5, 5.5}, the highest index in array list is ________.
- A4
- B2
- C3
- D0
- E1
166Suppose a method p has the following heading:
public static int[ ] p()
What return statement may be used in p()?- Areturn int[ ]{1, 2, 3};
- Breturn {1, 2, 3};
- Creturn 1;
- Dreturn new int[ ]{1, 2, 3};
167Analyze the following code:
public class Test {
public static void main(String[ ] args) {
int[ ] oldList = {1, 2, 3, 4, 5};
reverse(oldList);
for (int i = 0; i < oldList.length; i++)
System.out.print(oldList[i] + " ");
}
public static void reverse(int[ ] list) {
int[ ] newList = new int[list.length];
for (int i = 0; i < list.length; i++)
newList[i] = list[list.length - 1 - i];
list = newList;
}
}- AThe program displays 1 2 3 4 5.
- BThe program displays 1 2 3 4 5 and then raises an ArrayIndexOutOfBoundsException.
- CThe program displays 5 4 3 2 1.
- DThe program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException.
168 Analyze the following code: (Choose all that apply.)
public class Test {
public static void main(String[ ] args) {
A a = new A();
a.print();
}
}
class A {
String s;
A(String s) {
this.s = s;
}
void print() {
System.out.println(s);
}
}- AThe program has a compilation error because class A does not have a default constructor.
- BThe program would compile and run if you change A a = new A() to A a = new A("5").
- CThe program compiles and runs fine and prints nothing.
- DThe program has a compilation error because class A is not a public class.
169Which of the following statements are true? (Choose all that apply.)
- AData fields have default values.
- BYou may assign an int value to a reference variable.
- CLocal variables do not have default values.
- DA variable of a reference type holds a reference to where an object is stored in the memory.
- EA variable of a primitive type holds a value of the primitive type.
170Given the declaration Circle x = new Circle(), which of the following statement is most accurate?
- AYou can assign an int value to x.
- Bx contains a reference to a Circle object.
- Cx contains an int value.
- Dx contains an object of the Circle type.
171The keyword ________ is required to declare a class.
- Aprivate
- Bclass
- CAll of the above.
- Dpublic
172In this question, assume a class, Book, has been defined. Which set of statements creates an array of Book objects?
- ABook[] books;
books = new Book()[ numberElements ]; - BBook[] books;
books = new Book[ numberElements ]; - CAll of the above.
- Dnew Book() books[];
books = new Book[ numberElements ];
- ABook[] books;
173What is wrong in the following code?
class TempClass {
int i;
public void TempClass(int j) {
int i = j;
}
}
public class C {
public static void main(String[ ] args) {
TempClass temp = new TempClass(2);
}
}- AThe program has a compilation error because TempClass does not have a default constructor.
- BThe program has a compilation error because TempClass does not have a constructor with an int argument.
- CThe program compiles fine, but it does not run because class C is not public.
- DThe program compiles and runs fine.
174What is the default initial value of a String instance variable?
- A"default"
- B""
- Cnull
- Ddefault
175You should add the static keyword in the place of ? in line ________ in the following code:
1 public class Test {
2 private int age;
3
4 public ? int square(int n) {
5 return n * n;
6 }
7
8 public ? int getAge() {
9 }
10}- Ain both line 4 and line 8
- Bin line 8
- Cin line 4
- Dnone
176The boolean values can be displayed with the ________ format specifier.
- A%a.
- B%b.
- C%c.
- D%bool.
177What is the number of iterations in the following loop:
for (int i = 1; i <= n; i++) {
// iteration
}- An + 1
- B2*n
- Cn
- Dn - 1
178What modifier should you use on a class so that a class in the same package can access it but a class in a different package cannot access it?
- Aprotected
- Bprivate
- Cpublic
- DUse the default modifier.
179Which of the following is true?
- APseudocode is used to describe an algorithm.
- BPseudocode is translatable by the programmer into a programming language (like Java).
- CPseudocode is used to describe executable statements that will eventually be translated by the programmer into a program.
- DAll of the above.
180Which of the following is a double-selection control statement?
- Ado..while.
- Bfor.
- Cif...else.
- Dif.
181Suppose s1 and s2 are two strings. What is the result of the following code?
s1.equals(s2) == s2.equals(s1)- Atrue
- Bfalse
182A class within a package must be declared public if
- AIt will be used only by other classes in the same package.
- BIt will be used by classes that are not in the same package.
- CIt is in the same directory as the other classes in the package.
- DIt has a unique name.
183A String constructor cannot be passed variables of type:
- Achar arrays.
- Bint arrays.
- Cbyte arrays.
- DStrings.
184A programmer-defined constructor that has no arguments is called a ________.
- Azero-argument constructor.
- Bno-argument constructor.
- Cdefault constructor.
- Dmain constructor.
185Is the following loop correct?
for (; ; );- AYes
- BNo
186You can create an ArrayList using ________.
- AArrayList()
- Bnew ArrayList()
- Cnew ArrayList[100]
- Dnew ArrayList[ ]
187List the following operators in the order that they will be evaluated: -, *, /, +, %. Assume that if two operations have the same precedence, the one listed first will be evaluated first.
- A+, -, /, *, %.
- B-, +, %, *, /.
- C-, *, %, +, /.
- D*, /, %, -, +.
188Which of the following is not a Java primitive type?
- Achar
- Bbyte
- Creal
- Ddouble
189Which of the following statements are correct? (Choose all that apply.)
- AThe nextInt() method in the Random class returns the next random int value.
- BThe nextDouble() method in the Random class returns the next random double value.
- CWhen creating a Random object, you have to specify the seed or use the default seed.
- DIf two Random objects have the same seed, the sequence of the random numbers obtained from these two objects are identical.
190What exception type does the following program throw?
public class Test {
public static void main(String[ ] args) {
Object o = new Object();
String d = (String)o;
}
}- AClassCastException
- BNo exception
- CArrayIndexOutOfBoundsException
- DArithmeticException
- EStringIndexOutOfBoundsException
191Which of the following is not a benefit of goto-less programming?
- AEasier to debug and modify
- BShorter
- CClearer
- DMore likely to be bug free
192Consider integer array values, which contains 5 elements. Which statements successfully swap the contents of the array at index 3 and index 4?
- Avalues[ 3 ] = values[ 4 ];
values[ 4 ] = values[ 3 ]; - Bvalues[ 4 ] = values[ 3 ];
values[ 3 ] = values[ 4 ]; - Cint temp = values[ 3 ];
values[ 3 ] = values[ 4 ];
values[ 4 ] = temp; - Dint temp = values[ 3 ];
values[ 3 ] = values[ 4 ];
values[ 4 ] = values[ 3 ];
- Avalues[ 3 ] = values[ 4 ];
193To prevent a class from being instantiated, ________
- Ause the static modifier on the constructor.
- Buse the public modifier on the constructor.
- Cdon't use any modifiers on the constructor.
- Duse the private modifier on the constructor.
194When compiling a class in a package, the javac command-line option ________ causes the javac compiler to create appropriate directories based on the class's package declaration.
- A-p.
- B-a.
- C-d.
- D-dir.
195An advantage of inheritance is that:
- AAll methods can be inherited.
- BAll instance variables can be uniformly accessed by subclasses and superclasses.
- CObjects of a subclass can be treated like objects of their superclass.
- DNone of the above.
196An instance of ________ are unchecked exceptions. (Choose all that apply.)
- AThrowable
- BNumberFormatException
- CError
- DException
- ERuntimeException
197Assume StringBuilder strBuf is "ABCCEFC", after invoking ________, strBuf contains "ABTTEFT".
- AstrBuf.replace(2, 7, "TTEFT")
- BstrBuf.replace("CC", "TT")
- CstrBuf.replace("C", "T")
- DstrBuf.replace('C', "TT")
- EstrBuf.replace('C', 'T')
198Using public set methods provides data integrity if:
- AThe instance variables are public.
- BThe instance variables are private.
- CThe methods perform validity checking.
- DBoth b and c.
199Which of the following is the correct header of the main method? (Choose all that apply.)
- Apublic static void main(String[ ] x)
- Bpublic static void main(String args[ ])
- Cstatic void main(String[ ] args)
- Dpublic static void main(String x[ ])
- Epublic static void main(String[ ] args)
200What is x after the following statements?
int x = 1;
int y = 2;
x *= y + 1;- Ax is 2.
- Bx is 3.
- Cx is 1.
- Dx is 4.
Informações
- Descrição
- Practice quiz for Java 1 at Gwinnett Tech College.
- Autor
- Ummm No
- Idioma
- English
- Visualizações
- 1.263
- Etiquetas
- Avaliação
- Ainda sem avaliações Avalie este quiz