Option A and B will compile because protected and transient are legal variable modifiers. Option D will compile because volatile is a proper variable modifier.
Methods and variables are collectively known as members. Method and variable members are given access control in exactly the same way.
private makes a member accessible only from within its own class
protected makes a member accessible only to classes in the same package or subclass of the class
default access is very similar to protected (make sure you spot the difference) default access makes a member accessible only to classes in the same package.
public means that all other classes regardless of the package that they belong to, can access the member (assuming the class itself is visible)
final makes it impossible to extend a class, when applied to a method it prevents a method from being overridden in a subclass, when applied to a variable it makes it impossible to reinitialise a variable once it has been initialised
abstract declares a method that has not been implemented.
transient indicates that a variable is not part of the persistent state of an object.
volatile indicates that a thread must reconcile its working copy of the field with the master copy every time it accesses the variable.
After examining the above it should be obvious that the access modifier that provides the most restrictions for methods to be accessed from the subclasses of the class from another package is C - protected. A is also a contender but C is more restrictive, B would be the answer if the constraint was the "same package" instead of "any package" in other words the subclasses clause in the question eliminates default.
public class RTExcept { public static void throwit () { System.out.print("throwit "); throw new RuntimeException(); } public static void main(String [] args) { try { System.out.print("hello "); throwit(); } catch (Exception re ) { System.out.print("caught "); } finally { System.out.print("finally "); } System.out.println("after "); } }
A, B and C are incorrect based on the program logic described above. Remember that properly handled exceptions do not cause the program to stop executing.
Option B is incorrect because it is considered appropriate to check argument values in private methods using assertions.
Option C is incorrect; finally is never bypassed.
Option D is incorrect because AssertionErrors should never be handled.
Option B is wrong. wait() causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.
Option C is wrong. Methods of the InputStream class block until input data is available, the end of the stream is detected, or an exception is thrown. Blocking means that a thread may stop until certain conditions are met.
Option D is wrong. sleep() - Causes the currently executing thread to sleep (temporarily cease execution) for a specified number of milliseconds. The thread does not lose ownership of any monitors.
String a = "ABCD"; String b = a.toLowerCase(); b.replace('a','d'); b.replace('b','c'); System.out.println(b);
b.replace(char oldChar, char newChar);
But since this is only a temporary String it must either be put to use straight away i.e.
System.out.println(b.replace('a','d'));
Or a new variable must be assigned its value i.e.
String c = b.replace('a','d');
class Q207 { public static void main(String[] args) { int i1 = 5; int i2 = 6; String s1 = "7"; System.out.println(i1 + i2 + s1); /* Line 8 */ } }
If either operand is a String, the + operator concatenates the operands.
If both operands are numeric, the + operator adds the operands.
The expression on line 6 above can be read as "Add the values i1 and i2 together, then take the sum and convert it to a string and concatenate it with the String from the variable s1". In code, the compiler probably interprets the expression on line 8 above as:
System.out.println( new StringBuffer()
.append(new Integer(i1 + i2).toString())
.append(s1)
.toString() );
public class Test { public static void main(String [] args) { int [] [] [] x = new int [3] [] []; int i, j; x[0] = new int[4][]; x[1] = new int[2][]; x[2] = new int[5][]; for (i = 0; i < x.length; i++) { for (j = 0; j < x[i].length; j++) { x[i][j] = new int [i + j + 1]; System.out.println("size = " + x[i][j].length); } } } }
It produces 11 lines of output as given below.
D:\Java>javac Test.java D:\Java>java Test size = 1 size = 2 size = 3 size = 4 size = 2 size = 3 size = 3 size = 4 size = 5 size = 6 size = 7
Therefore, 11 is the answer.
(3) is an incorrect statement and therefore a correct answer because the hashcode for a string is computed from the characters in the string.
public class X { public static void main(String [] args) { String names [] = new String[5]; for (int x=0; x < args.length; x++) names[x] = args[x]; System.out.println(names[2]); } }and the command line invocation is
> java X a b
Comments
There are no comments.Copyright ©CuriousTab. All rights reserved.