From Practice Exam Questions -- Revature ROCP/OJA 1SO-811 Certification Preparation Course

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What is the numerical range of a byte?

-2 ^7 to 2^7-1

What is the prefix operator for incrementation/decrementation and what is the postfix operator for them?

1. Prefix: ++i, --i 2. Postfix: i++, i++

What will result from (int) (f + d) and ((int) f + (int) d)), respectively, from the following code? public class PromotionTest{ public static void main(String args[]) { int i = 5; float f = 5.5f; double d = 3.8; char c = 'a'; if (i == f) c++; if (((int) (f + d)) == ((int) f + (int) d)) c += 2; System.out.println(c); }}

9 and 8

What kind of exception will result from this code? class Node{ int id; Node node; public static void main(String[] args) { Node n = new Node(); System.out.println(n.id); System.out.println(n.node.id); } }

A NullPointerException because the value of n.node is null

Why can't a method be both abstract and static?

Because abstract means "Implements no functionality", and static means "There is functionality even if you don't have an object instance"

Why will if(bool == false){} (assuming bool is a valid variable in the right scope) compile but if(x = 3){} NOT compile?

Because if(bool == false){} returns a boolean but if(x = 3){} returns an integer, 3

What kind of statements are break and continue statements?

Control statements

True or False. There can only be one class type definition in a single java source file.

False, because of inner classes

Are constructors ever inherited?

No

What does the static import construct allow?

Unqualified access to static members of the imported class without inheriting from the type containing the static members; so just the members (both fields and methods???)

How are int nextInt() and int nextInt(int bound) different?

int nextInt(): Returns the next pseudorandom, uniformly distributed int value, uniformly distributed int value from this random number generator's sequence. All 2^32 possible int values are produced with (approximately) equal probability. int nextInt(int bound): Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

Which access modifiers can you use with a constructor?

public, private, and protected

What is "fall through" in switch statements?

If a case is caught, gains control, and executes but there is no break statement, all of the other cases after it will run, including any default statements that are after it. So always use a break statement unless you want fall through.

What kind of variables are typically used in constructor blocks?

Instance variables In object-oriented programming with classes, an instance variable is a variable defined in a class (i.e. a member variable), for which each instantiated object of the class has a separate copy, or instance. An instance variable is similar to a class variable.[1] An instance variable is a variable which is declared in a class but outside of constructors, methods, or blocks. Instance variables are created when an object is instantiated, and are accessible to all the constructors, methods, or blocks in the class. Access modifiers can be given to the instance variable.

What does the continue statement allow you to do in a loop?

It lets you: 1. Skip an iteration based on some control flow logic (conditional, etc.) and 2. Go to the next iteration in the loop

What is a label for a continue or break statement?

Label for a break statement: It's a variable that encloses functionality of specific, developer-specified control flow statement/s (loops) that the break statement interrupts and leaves completely, with any remaining control flow from the control flow statements preceding it completely stopped. Label for a continue statement: It's a variable that encapsulates functionality of specific, developer-specified control flow statement/s (loop/s) that the continue statement interrupts but does start where the first label is set ??? and continue the flow of the control flow statements (unlike break). Examples of break and continue statements with and without labels: BREAK WITHOUT LABEL: package org.mano.example; public class SimpleBreakDemo { public static void main(String[] args) { int counter = 0; for (int i = 0; i <= 10; i++) { for (int j = 0; j <= 10; j++) { if (i == 5) break; } counter++; } System.out.println(counter); } } Output: 11 BREAK WITH LABEL: package org.mano.example; public class LabeledBreakDemo { public static void main(String[] args) { int counter = 0; start: { for (int i = 0; i <= 10; i++) { for (int j = 0; j <= 10; j++) { if (i == 5) break start; } counter++; } } System.out.println(counter); } } Output: 5 CONTINUE WITHOUT LABEL: package org.mano.example; public class SimpleContinueDemo { public static void main(String[] args) { String[] listOfNames = { "Ravi", "Soma", "null", "Colin", "Harry", "null", "Smith" }; for (int i = 0; i < listOfNames.length; i++) { if (listOfNames[i].equals("null")) continue; System.out.println(listOfNames[i]); } } } //prints out lists of names except for "null" CONTINUE WITH LABEL: package org.mano.example; public class LabeledContinueDemo { public static void main(String[] args) { start: for (int i = 0; i < 5; i++) { System.out.println(); for (int j = 0; j < 10; j++) { System.out.print("#"); if (j >= i) continue start; } System.out.println("This will never" + " be printed"); } } }

Is an ArrayList more thread safe than an array, vice versa, or is neither of them thread safe?

Neither an ArrayList nor an array is thread safe. If you have multiple threads trying to add and remove elements from an ArrayList or an array, you have to write additional code to ensure thread safety.

Can static methods be abstract?

No

Can you change the order of an array with an enhanced for loop?

No

Can you skip an element in an enhanced for loop?

No

Can you use static, final, synchronized, native and abstract with a constructor?

No

Does an enhanced for loop give you an iterating variable that you can manipulate the way a for loop does?

No

Does it affect the control flow to put the default statement anywhere among the case statements in a switch-case statement and not at the end, which is most common?

No

At most how many class type definitions in a single Java source file can be public?

One (except for inner class files, but it's not on the exam)

How do you instantiate a Random object and then call it to print an integer between 0 and 10?

Random r = new Random(); System.out.print(r.nextInt(10));

The Random(long seed) constructor is equivalent to what code?

Random r = new Random(); r.setSeed(long seed);

How do you seed a Random object with 54 and then call it to print the result of the seed?

Random r = new Random(54); System.out.print(r);

What kind of error will this code make and why? public class Sum1 { public static void main(String[] args) { int sum; int i; sum = 0; // Initialize the running sum for ( i = 0; i < args.length; i++ ) { sum = sum + args[i]; // Add args[i] to running sum } System.out.println( sum ); } }

Sum1.java:13: incompatible types found : java.lang.String required: int sum = sum + args[i]; ^ 1 error because args is an array of Strings and args[i] doesn't refer to a string but an int as coded

What is interning of strings?

The way Java uses the same String object instead of creating a new one if one with the same value exists already (in the String pool)

How are continue statements and break statements similar, and how are they different?

They are similar in that they are control statements that interrupt the usual flow of the logic. They are different in how they function: A continue statement is used to end the current loop iteration and return control to the loop statement. Continue skips the current executing loop and moves to the next loop. A break statement moves out of the loop entirely and executes the next statement after the loop.

public class JavaTester { public int counter = 0; public static int staticCounter = 0; public JavaTester(){ counter++; staticCounter++; } public static void main(String args[]) { JavaTester tester = new JavaTester(); JavaTester tester1 = new JavaTester(); JavaTester tester2 = new JavaTester(); System.out.println("Counter: " + tester2.counter); System.out.println("Static Counter: " + tester2.staticCounter); } } Output Counter: 1 Static Counter: 3

When a new instance is made, the non static counter increments because non static instance variables are

How are static import declarations analogous to normal import declaration?

Where the normal import declaration imports classes from packages, allowing them to be used without package qualification, the static import declaration imports static members from classes, allowing them to be used without class qualification.

What do you have to do if you have multiple threads trying to add and remove elements from an ArrayList or an array to ensure thread safety.

Write additional code

Can there be multiple classes in the same file as long as only one of the class is public?

Yes

Is it allowed to assign a non-static field's value to a static field?

Yes

Is it allowed to assign a static field's value to a non-static field?

Yes

In this code, does i ever increment? public class TestClass{ public static void main(String args[]) { int i; int j; for (i = 0, j = 0 ; j < 1 ; ++j , i++) { System.out.println( i + " " + j ); } System.out.println( i + " " + j ); }}

Yes ... the final result is 1 and 1. how??? can you say i and j are set as 0 and increment at the same time??? It seems the comma means "and then do this"

Which of these statements regarding the following code are correct ?public class TestClass{static int a;int b;public TestClass(){int c;c = a;a++;b += c;}public static void main(String args[]) { new TestClass(); }} Answered Incorrectly You had to select 1 option(s) The code will fail to compile, since the constructor is trying to access static members. The code will fail to compile, since the constructor is trying to use static member variable a before it has been initialized. The code will fail to compile, since the constructor is trying to use member variable b before it has been initialized. The code will fail to compile, since the constructor is trying to use local variable c before it has been initialized.c is getting initialized at line 2: c = a; The code will compile and run without any problems. PreviousNextEvaluateFinishReview All instance and static variables of a class are given default values, if not explicitly initialized. All numeric variables are given a value of zero or equivalent to zero (ie. 0.0 for double and float), boolean variables are assigned false, and reference variables are assigned null. Can you declare a variable in a constructor?

Yes, but only if you're reassigning a member variable to it.

Is the code inside the condition of this for loop correct? for ( ; i<5?false:true ; );

Yes. It may seem unusual for a ternary statement to go in the condition of a for loop, but it's not incorrect because the ternary statement, which is correctly made in the example, returns a boolean and booleans are the only acceptable return value for the condition of a for loop. It may also seem unusual for there to be parts of the condition of a for loop that are blank. But this is not a problem in and of itself as long as the two semicolons are present. It may also seem unusual for the for and the condition of the for loop not to be followed by the curly braces of the block body. However, as with if statements, curly braces are not necessary.

Will this compile if TestClass is the name of the class for public void TestClass(int a) {}?

Yes. It is interesting to note that public void TestClass(int a) {} // 2 will actually compile. It is not a constructor, but compiler considers it as a valid method!

What kind of an exception will result from using the wrong kind of data type for format specifiers (%d, %s, etc.)?

java.util.IllegalFormatConversionException

What will be printed if java CommandLinePractice.java dog cat mouse is run on the command line? package dev.distasio.miscellaneous_methods; public class CommandLinePractice { public static void main(String[] args) { int i = args.length; // while (i > 0) { // i--; // System.out.println(i); // } while (i > 0) { i--; System.out.println(args[i]); } } }

mouse cat dog (parameters go in last first)

How to fix the error in this code: public class Sum1 { public static void main(String[] args) { int sum; int i; sum = 0; // Initialize the running sum for ( i = 0; i < args.length; i++ ) { sum = sum + args[i]; // Add args[i] to running sum } System.out.println( sum ); } }

public class Sum2 { public static void main(String[] args) { int sum; int i; sum = 0; // Initialize the running sum for ( i = 0; i < args.length; i++ ) { sum = sum + Integer.parseInt(args[i]); // Add args[i] to running sum } System.out.println( sum ); } }

What do the different parts of this command line compiling TestClass mean? java -classpath . a.a.a.TestClass

-classpath: a parameter in the Java Virtual Machine or the Java compiler that specifies the location of user-defined classes and packages. The parameter may be set either on the command-line, or through an environment variable https://en.wikipedia.org/wiki/Classpath#:~:text=Classpath%20is%20a%20parameter%20in,or%20through%20an%20environment%20variable. . : represents the current directory for the given package, classname, etc. before the given package (eg., <currentdir>/a/a/a/TestClass.class) a.a.a.TestClass: the package of the class and the class to be compiled

What kinds of files do these directories typically contain? /bin, /lib, and /src

/bin - empty folder that will contain compiled .class files /lib - contains third party .jar files /src - contains .java source files

What is the numerical range of a char?

0 - 2^16-1

What is the syntax for the Random class's two constructors, and what do they do?

1. Random(): Creates a new random number generator 2. Random(long seed): Creates a new random number generator using a single long seed

Which two Random class methods return an int and how do they work?

1. int nextInt(): Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence. All 2^32 possible int values are produced with (approximately) equal probability 2. int nextInt(int bound): Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

Why in the range of numerical values for a long (for example)( -2 ^63 to 2^63-1), is the signed part ^63 but the unsigned part is ^63-1?

Because the signed numbers occur before zero and the ^63-1 includes zero

Why does this code give a warning?

Because using the class identifier (name of the class) for a static variable rather than this is best practice (though it's technically correct and you only get a warning in Eclipse for it). That is because a class identifier is better this is more appropriate as a reference to an specific instance object than is

Are static imports for importing just one individual member of a class, or can multiple static imports from a class be imported?

Both individually (eg., import static java.lang.Math.PI;) or en masse: (eg., import static java.lang.Math.*;) So when should you use static import? Very sparingly! Only use it when you'd otherwise be tempted to declare local copies of constants, or to abuse inheritance (the Constant Interface Antipattern). In other words, use it when you require frequent access to static members from one or two classes. If you overuse the static import feature, it can make your program unreadable and unmaintainable, polluting its namespace with all the static members you import. Readers of your code (including you, a few months after you wrote it) will not know which class a static member comes from. Importing all of the static members from a class can be particularly harmful to readability; if you need only one or two members, import them individually. Used appropriately, static import can make your program more readable, by removing the boilerplate of repetition of class names.


संबंधित स्टडी सेट्स

Quickbooks Online Domain 3 and 4

View Set

7.4: Religion and religious ideas: Catholicism

View Set

CIS 105 Sound Byte: Installing a Home Computer Network

View Set

PrepU Chapter 19: Management of Patients with Chest and Lower Respiratory Tract Disorders

View Set

Penny : CH 24 Fetal Head and Brain

View Set