Abstract Classes and Interfaces Quiz
Abstract Classes and Interfaces Quiz
[Link] is a valid method signature in an interface? implements Threads and contain a method called
a)private int getArea(); run().
b)protected float getVol(float x); d)The mandatory method must be public, with a
c) public static void main(String [] args); return type of void, must be called run(), and cannot
d)boolean setFlag(Boolean [] test []); take any arguments.
[Link] statement is true about interfaces? [Link] is a valid declaration within an interface?
a)Interfaces allow multiple implementation a)protected short stop = 23;
inheritance. b)final void madness(short stop);
b)Interfaces can extend any number of other c)public Boolean madness(long bow);
interfaces. d)static char madness(double duty);
c) Members of an interface are never static.
d)Members of an interface can always be declared [Link] an abstract class define both abstract methods
static. and non-abstract methods ?
a)No-it must have all one or the other.
[Link] statement is true about interfaces? b) No-it must have all abstract methods.
a)The keyword extends is used to specify that an c)Yes-but the child classes do not inherit the abstract
interface inherits from another interface. methods.
b) The keyword extends is used to specify that a class d)Yes-the child classes inherit both.
inherits from an interface. [Link] one of the following statements is true ?
c) The keyword implements is used to specify that an a) An abstract class can be instantiated.
interface inherits from another interface. b)An abstract class is implicitly final.
d)The keyword implements is used to specify that a c)An abstract class can declare non-abstract methods.
class inherits from another class. d)An abstract class can not extend a concrete class.
[Link] of the field declaration is legal within the [Link] is an abstract method?
body of an interface? a)An abstract method is any method in an abstract
a) protected static int answer = 42; class.
b)volatile static int answer = 42; b)An abstract method is a method which cannot be
c)int answer = 42; inherited.
d)private final static int answer = 42; c)An abstract method is one without a body that is
declared with the reserved word abstract.D)An
[Link] declaration prevents creating a subclass of a abstract method is a method in the child class that
top level class?
overrides a parent method.
a) private class Javacg{}
b)abstract public class Javacg{} [Link] is an abstract class?
c)final public class Javacg{} a)An abstract class is one without any child classes.
d)final abstract class Javacg{} b)An abstract class is any parent class with more than
one child class.
[Link] is an abstract method defined in the parent: c)An abstract class is a class which cannot be
public abstract int sumUp ( int[] arr ); instantiated.
Which of the following is required in a non-abstract d)An abstract class is another name for "base class."
child?
a)public abstract int sumUp ( int[] arr ) { . . . } [Link] declaration in the below code represents a
b)public int sumUp ( int[] arr ) { . . . } valid declaration within the interface ?
c)public double sumUp ( int[] arr ) { . . . } 1. public interface TestInterface {
d)public int sumUp ( long[] arr ) { . . . } 2. volatile long value=98L;
3. transient long amount=67L;
[Link] statement is true for any concrete class 4. Long calculate(long input);
implementing the [Link] interface? 5. static Integer getValue();
a)The class must contain an empty protected void 6. }
method named run(). a)Declaration at line 2.
b)The class must contain a public void method named b)Declaration at line 3.
runnable().
1
c)Declaration at line 4. 1. interface I1 {
d)Declaration at line 5. 2. int process();
3. }
[Link]: 4. class C implements I1 {
1. public interface Constants { 5. int process() {
2. static final int SEASON_SUMMER=1; 6. [Link]("process of C invoked");
3. final int SEASON_SPRING=2; 7. return 1;
4. static int SEASON_AUTUMN=3; 8. }
5. public static const int SEASON_WINTER=4; 9. void display() {
6. } 10. [Link]("display of C invoked");
What is the expected behaviour on compiling the 11. }
above code? 12.}
a)Compilation error occurs at line 2. [Link] class TestC {
b)Compilation error occurs at line 3. 14. public static void main(String... args) {
c)Compilation error occurs at line 4. 15. C c = new C();
d)Compilation error occurs at line 5. 16. [Link]();
17. }
[Link] the following, 18.}
1. abstract class A { What is the expected behaviour?
2. abstract short m1() ; a)Compilation error at line 5.
3. short m2() { return (short) 420; } b)Compilation error at line 9.
4. } c) Runtime error occurs.
5. d)Prints "process of C invoked".
6. abstract class B extends A {
7. // missing code ? [Link]:
8. short m1() { return (short) 42; } 1. public interface Alpha {
9. } 2. String MESSAGE = "Welcome";
Which of the following statements is true? 3. public void display();
a) Class B must either make an abstract declaration of 4. }
method m2() or implement method m2() to allow the To create an interface called Beta that has Alpha as its
code to compile. parent, which interface declaration is correct?
b) It is legal, but not required, for class B to either a)public interface Beta extends Alpha { }
make an abstract declaration of method m2() or b)public interface Beta implements Alpha {}
implement method m2() for the code to compile. c)public interface Beta instanceOf Alpha {}
c) As long as line 8 exists, class A must declare method d)public interface Beta parent Alpha { }
m1() in some way.
d) If class A was not abstract and method m1() on line [Link]:
2 was implemented, the code would not compile. 1. abstract class AbstractClass {
2. void setup() { }
[Link] the following, 3. abstract int execute();
1. interface Base { 4. }
2. boolean m1 (); 5. class EC extends AbstractClass {
3. byte m2(short s); 6. int execute() {
4. } 7. [Link]("execute of EC invoked");
Which of the following code fragment will compile? 8. return 0;
a)interface Base2 implements Base {} 9. }
b) abstract class Class2 extends Base { 10.}
public boolean m1() { return true; } } [Link] class TestEC {
c) abstract class Class2 implements Base { 12. public static void main(String... args) {
public boolean m1() { return (7 > 4); } } 13. EC ec = new EC();
d) class Class2 implements Base { 14. [Link]();
boolean m1() { return false; } 15. [Link]();
byte m2(short s) { return 42; } } 16. }
17.}
[Link]: What is the expected behaviour?
2
a)Compilation error at line 2. c)1 2 3 4
b)Compilation error at line 14. d)Compilation fails
c)Runtime error occurs.
d)Prints "execute of EC invoked". [Link] the following,
1. interface DoMath {
[Link] the code below: 2. double getArea(int rad); }
interface MyInterface { 3.
void doSomething ( ) ; 4. interface MathPlus {
} 5. double getVol(int b, int h); }
class MyClass implements MyInterface { 6.
// xx
}
7.
Choose the valid option that can be substituted in 8.
place of xx in the MyClass class . Which code fragment inserted at lines 7 and 8 will
a)public native void doSomething ( ) ; compile?
b) void doSomething ( ) { /* valid code fragments */ } a) class AllMath extends DoMath {
c)private void doSomething ( ) { /* valid code double getArea(int r); }
fragments */ } b)interface AllMath implements MathPlus {
d)protected void doSomething ( ) { /* valid code double getVol(int x, int y); }
fragments */ } c)interface AllMath extends DoMath {
float getAvg(int h, int l); }
[Link] I1 { d)class AllMath implements MathPlus {
void draw(); double getArea(int rad); }
}
class C implements I1 { [Link] I1 {}
xxxxxx interface I2 {}
} class Base implements I1 {}
Which of the following when inserted at xxxxxx is a class Sub extends Base implements I2 {}
legal definition and implementation ? class Red {
a)void draw() { } public static void main(String args[]) {
b)public void draw() { } Sub s1 = new Sub();I2 i2 = s1; // 1
c)protected void draw() { } I1 i1 = s1; // 2
d)abstract void draw() { } Base base = s1; // 3
Sub s2 = (Sub)base; // 4
[Link] the following,
1. interface Count { }
2. short counter = 0; }
3. void countUp(); A compile-time error is generated at which line?
4. } a) 2
5. public class TestCount implements Count { b) 3
6. c) 4
7. public static void main(String [] args) { d) No error will be generated.
8. TestCount t = new TestCount();
9. [Link](); [Link]:
10. } 1. public interface IDrawable {
11. public void countUp() { 2. static final int SHAPE_CIRCLE=1;
12. for (int x = 6; x>counter; x--, ++counter) { 3. final int SHAPE_SQUARE=2;
13. [Link](" " + counter); 4. static int SHAPE_RECTANGLE=3;
14. } 5. public static const int SHAPE_TRIANGLE=4;
15. } 6. }
16. } What is the expected behaviour on compiling the
What is the result? above code?
a)1 2 3 a) Compilation error occurs at line 2.
b)0 1 2 3 b) Compilation error occurs at line 3.
3
c) Compilation error occurs at line 4. [Link] of the following member level (i.e.
d) Compilation error occurs at line 5. nonlocal) variable declarations will not compile?
a) transient int b = 3;
[Link]: b) public static final int c;
1. abstract class MyClass { c) volatile int d;
2. void init() { } d) private synchronized int e;
3. abstract int calculate();
4. } [Link] of the following modifiers can be applied
5. class MyImpl extends MyClass { to the declaration of a field?
6. int calculate() { a) abstract
7. [Link]("Invoking calculate..."); b) volatile
8. return 1; c) native
9. } d) synchronized
10.}
[Link] class TestMyImpl { [Link] statement is true about the use of
12. public static void main(String[] args) { modifiers?
13. MyImpl mi = new MyImpl(); a) If no accessibility modifier (public, protected, and
14. [Link](); private) is specified for a member declaration, the
15. [Link](); member is only accessible for classes in the same
16. } package and subclasses of its class in any package.
17.} b) You cannot specify accessibility of local variables.
What is the expected behaviour? They are only accessible within the block in which
a) Prints "Invoking calculate...". they are declared.
c) Subclasses of a class must reside in the same
package as the class they extend.
b) Runtime error occurs. d) Local variables can be declared static.
c) Compilation error at line 2.
d) Compilation error at line 14. [Link] is the most restrictive access modifier that
will allow members of one class to have access to
members of another class in the same package?
Topic: Access Modifiers a) abstract
b) protected
[Link] one of the following modifiers can be c) synchronized
applied to a method? d) default access
a) transient
b) native [Link] statement is true?
c) volatile a) Constructors can be declared abstract.
d) friend b) A subclass of a class with an abstract method must
provide an implementation for the abstract method.
[Link] a method in a class, what access modifier c) Transient fields will be saved during serialization.
do you use to restrict access to that method to only d) Instance methods of a class can access its static
the other members of the same class? members implicitly.
a) static
b) private [Link] statement is true?
c) protected
d) volatile a) A static method can call other non-static methods
in the same class by using the this keyword.
[Link] of the following modifiers can be applied b) A class may contain both static and non-static
to a constructor? variables and both static and non-static methods.
a) protected c) Each object of a class has its own instance of each
b) static static variable.
c) synchronized d) Instance methods may access local variables of
d) transient static methods.
4
[Link] of the following modifiers cannot be a) final
applied to a top level class? b) transient
a) public c) volatile
b) private d) synchronized
c) abstract
d) final [Link] restrictive is the default accessibility
compared to public, protected, and private
[Link] of the following modifiers cannot be accessibility?
applied to a method? a) Less restrictive than public.
a) final b) More restrictive than public, but less restrictive
b) synchronized than protected.
c) transient c)More restrictive than protected, but less restrictive
d) native than private.
d)More restrictive than private.
[Link] of the following modifiers can be applied [Link] is printed out following the execution of the
to a constructor? code below ?
a)private 1. class Test {
b) abstract final volatile 2. static String s;
3. public static void main(String []args) {
[Link] statement is true about modifiers? 4. int x = 4;
a) Fields can be declared native. 5. if (x < 4)
b) Non-abstract methods can be declared in abstract 6. [Link]("Val = " + x);
classes. 7. else
c) Classes can be declared native. 8. [Link](s);
d) Abstract classes can be declared final. 9. }
10. }
[Link] which of the following can the keyword a) Nothing. The code fails to compile because the
"synchronized" be placed, without causing a compile String s isn’t declared correctly.
error. b) The text "Val = null" is displayed.
a) class variables c) The text "null" is displayed.
b) instance methods d) Runtime error due to NullPointer exception.
c) instance variables
d) a class [Link] the following 2 classes and select the
correct statement.
41.A protected method can be overridden by class A {
a) A private method private int x = 0;
b) A method without any access specifiers (i.e. static int y = 1;
default) protected int q = 2;
c) A protected method }
d) All of the above class B extends A {
void method() {
[Link] statement is true about accessibility of [Link](x);
members? [Link](y);
a) Private members are always accessible from within [Link](q);
the same package. }
b)Private members can only be accessed by code from }
within the class of the member. a) The code fails to compile because the variable x is
c) A member with default accessibility can be not available to class B.
accessed by any subclass of the class in which it is b) The code compiles correctly, and the following is
defined. displayed:012
d) Package/default accessibility for a member can be c) The code fails to compile because you can’t
declared using the keyword default. subclass a class with protected variables.
d) The code fails to compile because you can’t
[Link] of the following modifiers cannot be subclass a class with static variables.
applied to the declaration of a field?
5
[Link] the following class, which of these is valid }
way of referring to the class from outside of the public static void main(String [] args) {
package [Link]?
package [Link]; int k = [Link](5,10);
public class MyClass { [Link](k);
// ... }
} }
a) By simply referring to the class as MyClass. What is the result?
b) By simply referring to the class as [Link]. a) 70
c) By simply referring to the class as [Link]. b) 52
d) By importing with com.* and referring to the class c) Compilation error
as [Link]. d) An exception is thrown at runtime
[Link] is the value of [Link] for the [Link] type parameter must the following method
following array? be called with?
String[] seasons = {"winter", "spring", "summer", int myMethod ( double[] ar )
"fall", }; {
undefined ....
a) 4 }
b) 5 a) An empty double array.
c) 22 b) A reference to an array that contains elements of
type double.
[Link] of the following will declare an array and c) A reference to an array that contains zero or more
initialize it ? elements of type int.
a) Array a = new Array(5); d) An array of any length that contains double and
b) int array[] = new int [5]; must be named ar
c) int a[] = new int(5); .
d)int [5] array; [Link] the declaration:
char[] c = new char[100];
[Link] of the following is an illegal declaration of What is the value of c[50]?
array ? a) 49
a) int [] myscore[]; b) 50
b) char [] mychars; c) ‘\u0020’
c) Dog mydogs[7]; d) ‘\u0000’
d) Dog mydogs[];
[Link] will legally declare, construct, and initialize
[Link] will legally declare, construct, and initialize an array?
an array? a) int [] myList = {"9", "6", "3"};
a) int [] myList = {"5", "8", "2"}; b) int [3] myList = (9, 6, 3);
b) int [3] myList = (5, 8, 2); c) int myList[] [] = {9,6,3,0};
c) int myList[] [] = {5,8,2,0}; d) int [] myList = {9, 6, 3};
d) int [] myList = {5, 8, 2}; [Link] the following code snippet:
float average[] = new float[6];
[Link] of these array declaration statements is Assuming the above declaration is a local variable in a
not legal? method of a class, after the above statement is
a) int[] i[] = { { 1, 2 }, { 1 }, {}, { 1, 2, 3 } }; executed, which of the following statement is false ?
b) int i[][] = new int[][] { {1, 2, 3}, {4, 5, 6} }; a) [Link] is 6
c) int i[4] = { 1, 2, 3, 4 }; b) average[0] is 0.0
d) int i[][] = { { 1, 2 }, new int[ 2 ] }; c) average[5] is undefined
d) average[6] is undefined
[Link] of the following is a legal declaration of a
two-dimensional array of integers? Topic: Assignments, Expressions, Operators
a) int[5][5]a = new int[][];
b) int a = new int[5,5]; [Link] one of the below expression is equivalent
c) int[]a[] = new int[5][]; to 16>>2 ?
d) int[][]a = new int[][5]; a) 16/4
b) 16/2
8
c) 16*2 c) x >>> 1;
d) 16/2^2 d) x << -1;
[Link] of the following is correct? [Link] you have four int variables: x, y, z, and
a) 8 >> 2 gives 2 result.
b) 16 >>> 2 gives 2 Which expression sets the value of z to x if result has a
c) 4 << 2 gives 2 value of 1, and the value of y to x otherwise?
d) 2 << 1 gives 2 a) x = (result == 1) ? z : y;
b) x = (result == 1) ? y : z;
[Link] of the following is correct? c) x = (result == 1) : y ? z;
128>>> 1 gives d) x = (result == 1) : z ? y;
a) 32
b)64 [Link] a variable x of type int ( which can contain
c) -64 a negative value), which of these expressions always
d) -32 gives a positive number irrespective of the value of
x?
[Link] is the value of -32 % 6 ? a) x << 1;
a) 5 b) x >> 1;
b) -5 c) x >>> 1;
c) 2 d) x << 2;
d) -2
[Link]:
[Link] one of the following is a short-circuit int x = 7;
operator ? x <<= 2;
a)| What best describes the second line of code?
b) && a) It assigns the value of 2 to x, and shifts it to left by
c) & one place.
d) ^ b) It assigns the value to x after shifting x to 2 places
left.
[Link] the following code snippet: c) It assigns the value to x after shifting 2 to x places
double sum = 10.0, price=100; left.
sum += price>=100 ? price*1.1 : price; d) It is invalid because there is no such operator as
What value is placed in sum? Choose the most <<=.
appropriate answer.
a) 90 [Link] the variables defined below:
b) 100 int one = 1;
c) 110 int two = 2;
d) 120 char initial = ‘2’;
boolean flag = true;
[Link] x, y, and z are all integers, which expression will Which one of the following is invalid?
produce a runtime error? a) if( one == two ){}
NOTE: The expressions are always evaluated with all b) switch( one ){}
the integers having a value of 1. c) switch( flag ){}
a) z = x/y--; d) switch( initial ){}
b) z = -x/x;
c) z = y/x--; [Link] the shift operator that returns -1 as the
d) z = y%--x value of the variable in the following statement:
int a= -4 MISSING OPERATOR 2;
[Link] a variable x of type int ( which contains a a) >>>
positive value), which is the correct way of doubling b) >>
the value of x, barring any wrapping of out-of-range c) <<<
intermediate values ? d) <<
[Link] the following: [Link] you are writing code for a for loop that
1. class ShapeException extends Exception {} must execute three [Link] is the correct
2. declaration for the loop?
3. class CircleException extends ShapeException {} a) for (int i < 4; i = 1; i++)
b) for (int i = 0; i < 4; i++)
4. c) for (int i = 1; i++; i < 4)
5. public class Circle1 { d) for (int i = 3; i >=1; i--)
6. void m1() throws CircleException {throw new
ShapeException();} [Link] flow control mechanism determines when
7. a block of code should run more than once?
8. public static void main (String[] args) { a) iteration
9. Circle1 circle1 = new Circle1(); b) sequence
10. int a=1, b=1; c) selection
11. d) exceptions
12. try {circle1.m1(); a--;} catch (CircleException e)
{b--;} [Link] of the following is a legal loop definition?
13. a) while (int a == 0) { /* whatever */ }
14. [Link]("a=%d, b=%d", a, b); b) do { /* whatever */ } while (int a = 0);
15. } c) do { /* whatever */ } while (int a == 0);
16.} d) for (int a=0; a<100; a++) { /* whatever */ }
What is the expected output ?
[Link] the following code:
a) a=1, b=1 public class SwitchTest {
b) a=0, b=1 public static void main(String [] args) {
c) a=1, b=0 int i=5, j=0;
d)Compile time error at line 6. switch(i){
case 2: j+=3;
Topic: Flow Control case 4: j+=5;
default : j+=1;
[Link] your code needs to traverse through an case 0: j+=7;
array named array1. Which code would you use to do }
this ? [Link]("j value " + j);
a) for(int i = 0; i <= [Link]; i++) }
b)for(int i = 0; i < [Link]; i++) }
c) for(int i = 0; i <= [Link](); i++) What is the result?
d) for(int i = 0; i < [Link](); i++) a) j value 16
b) j value 8
[Link] of the following is legal? c) j value 7
a)for (int i=0, j=1; i<10; i++, j++) { } d) Compilation error ( "default" should be at the last
b) for (int i=0, j=1; i<10; i++; j++) { } of the switch statement)
c) for (int i=0, j=1; i<10,j<10; i++, j++) { }
d) for (int i=0, float j=1.0; ; i++, j++) { } [Link] the following code:
public class TestBreak {
22
public static void main(String [] args) { value is three
int i = 2; d) value is 2
if (i < 2) {
i++; [Link] the following:
break printAndExit; public class TestLoop
} {
i++; public static void main(String... args)
printAndExit: {
[Link](i); int index = 2;
} while( --index > 0 )
} [Link]( index );
What will be the result of the above code? }
a) 2 }
b) 3 What is printed to standard output?
c) 4 a)1
d) Compilation error 0
b) 1
[Link] the following: 2
public class DoWhileTest { c) 1
public static void main(String [] args) { d) Nothing is printed
int i=2, j=5;
do [Link] the following,
{ 1. int i = 0;
if(i++ > --j) continue; 2. label:
}while(i < 3); 3. if (i < 2) {
[Link]("i=%d, j=%d",i,j); 4. [Link](" i is " + i);
} 5. i++;
} 6. continue label;
After execution, what are the values of i and j? 7. }
a) i=4, j=4 What is the result?
b) i=3, j=4 a) Compilation fails.
c) i=2, j=4 b) Produces no output
d) i=2, j=5 c) i is 0
d) i is 0 i is 1
[Link] statement is true about the following code
fragment? [Link] the following:
1. int j = 2; public class TestLoop {
2. switch (j) { public static void main(String... args) {
3. case 2: outer: for( int i = 0; i <2; i++ )
4. [Link]("value is two"); { inner: for( int j = 0; j < 2; j++ )
5. case 2 + 1: { if( j==1 )
6. [Link]("value is three"); continue outer;
7. break; [Link]( "i=%d, j=%d\n",i,j);
8. default: }
9. [Link]("value is " + j); }
10. break; }
11. } }
The code is illegal because of the expression at line 5. What is printed to standard output?
a) The output would be a) i=0, j=0
value is two b) i=0, j=0
b) The output would be i=1, j=0
value is two c) i=0, j=0
value is three i=0, j=1
c) The output would be d) i=0, j=0
value is two i=1, j=1
23
int i; // Line 3
[Link] the following code: for (i=0; i<1; i++) {[Link](i);} //
public class PESTest { Line 4
public static void main (String[] args) { [Link](TestFor.i);
int j = 0; }}
do for (int i = 0; i++ < 2;) What is the result of attempting to compile and run
[Link](i); the program?
while (j++ < 1); a) Prints: 1100
} b) Prints: 1102
} c) Compile-time error at Line 1
What is the result of attempting to compile and run d) Compile-time error at Line 4
the program?
a) Prints: 12 [Link] the code snippet:
b)Prints: 1212 int m = 0;
c) Prints: 121212 while( ++m < 2 )
d) Compile-time error [Link]( m );
What is printed to standard output?
[Link]: a) 0
switch( i) b)1
{ c) 2
default : d) Nothing is printed
[Link]("Hello");
} [Link] the following code:
What is the acceptable type for the variable i? public class TestForSwitch {
public static void main (String[] args) {
a) byte for (int i = 0; i < 3; i++) {
b) float switch (i) {
c) double default: [Link]("D");
d) Object case 0: [Link]("0");
case 1: [Link]("1");
[Link] the following code: }}}}
public class TestSwitch { What is the result of attempting to compile and run
public static void main(String args[]) { the program?
byte b = -1; a) Prints: DDD
switch(b) { b) Prints: 01D
case -1: [Link]("-1"); break; c) Prints: 01D01
case 127: [Link]("127"); break; d)Prints 011D01
case 128: [Link]("128"); break;
default: [Link]("Default "); [Link] the following code:
}}} class SwitchTest {
What is the result of attempting to compile and run public static void main(String args[]) {
the program? int x = 3; int success = 0;
a) Prints: -1 do {
b) Prints: 128 switch(x) {
c) Prints: Default case 0: [Link]("0"); x += 5; break;
d) Compile-time error case 1: [Link]("1"); success++;
break;
[Link] the following code: case 2: [Link]("2"); x += 1; break;
public class TestFor { case 3: [Link]("3"); x -= 2; break;
static int i; default: break;
public static void main(String args[]) { }
for (i=1; i<2; i++) {[Link](i);} // } while ((x != 1) || (success < 2));
Line 1 }}
for (int i=1; i<2; i++) {[Link](i);} // What is the result of attempting to compile and run
Line 2 the program?
24
a) Prints: 3631 a) 3210
b) Prints: 3621 b)321
c) Prints: 311 c) Will go into an infinite loop
d) Compile-time error d)Compilation error
34
[Link]: 3. public static void main(String[] args)
1. public class Test { 4. {
2. public static void main(String[] args) { 5. byte b1=198;
3. unsigned byte b=0; 6. byte b2=1;
4. b--; 7. [Link](b1+b2);
5. 8. }
6. } 9. }
7. } a) Compilation error at Line 5.
What is the value of b at line 5? b) Compilation error at Line 7.
a) -1 c) Prints 199
b) 255 d) Prints a number different from 199.
c)Compilation error at line 3 as there is nothing like
unsigned byte in Java. [Link] results would print from the following code
d) Compilation succeeds and throws runtime snippet:[Link]("12345 ".valueOf(54321));
exception at line 4. a) 12345 54321
b)54321
[Link] is the result of compiling and executing the c) The application won’t compile.
below code ? d) Runtime error
1. public class Test
2. { [Link]:
3. public static void main(String[] args) 1. public class ValueCheck {
4. { 2. public static void main(String[] args) {
5. byte b=127; 3. unsigned byte y = -1;
6. byte c=15; 4. y++;
7. byte a = b + c; 5.
8. } 6. }
9. } 7. }
a) Throws runtime exception at line no 7 saying "out What is the value of y at line 5?
of range". a) 0
b) Compilation succeeds and a takes the value of 142. b) 2
c) Compilation error at line 5. Byte cant take value of c)Compilation error at line 3 as there is nothing like
127. unsigned byte in Java.
d) Compilation error at line 7. d) Compilation succeeds and throws runtime
exception at line 4.
[Link] will be the output after compiling the
following statements? [Link] is the result of compiling and executing the
public class TestIdentifier below code ?
{ 1. public class ByteTest
public static void main(String[] args) 2. {
{ 3. public static void main(String[] args)
double volatile = 21+3.775; 4. {
[Link](volatile); 5. byte x=100;
} 6. byte y=127;
} 7. byte z = x + y;
8. }
a) 25 9. }
b) 24.775 a) Throws runtime exception at line no 7 saying "out
c) 24 of range".
d) Compilation error as volatile is a keyword and b) Compilation succeeds and a takes the value of 227.
cannot be used as identifier. c) Compilation error at line 6. Byte cant take value of
127.
[Link] is the result of compiling and executing the d)Compilation error at line 7.
below code ?
1. public class Test [Link] will be the output after compiling the
2. { following statements?
35
public class TestIdentifier
{ [Link] range of values is valid for all integral
public static void main(String[] args) types, where n is the number of bits?
{ a) 2^(n-1) to 2^(n+1)+1
float volatile = 53+4.289; b) -2^(n-1) to 2^(n-1)-1
[Link](volatile); c) -2^(n-1) to 2^(n-1)+1
} d) -2^(n)-1 to 2^(n-1)-1
}
a) 58 [Link] char c = ‘A’;
b) 57.289 What is the simplest way to convert the character
c) 57 value in c into an int?
d) Compilation error as volatile is a keyword and a) int i = [Link](c);
cannot be used as identifier. b) int i = (int) c;
c) int i = int (c);
[Link] is the result of compiling and executing the d) int i = c;
below code ?
1. public class Test [Link] primitive type ranges from -2^31 to
2. { (2^31)-1?
3. public static void main(String[] args) a) long
4. { b) int
5. byte y1=3; c) short
6. byte y2=225; d) byte
7. [Link](y1+y2);
8. } [Link] primitive type char in Java consists of
9. } a) 8 bits
a) Compilation error at Line 6. b)16 bits
b) Compilation error at Line 7. c) 24 bits
c) Prints 228 d) 32 bits
d) Prints a number different from 228.
[Link] which of these variable declarations will the
[Link] results would print from the following code variable remain uninitialized unless explicitly
snippet:[Link]("ABCDE ".valueOf(98765)); initialized?
a) ABCDE 98765 a) Declaration of an instance variable of type boolean
b)98765 b) Declaration of a static variable of type double
c) The application won’t compile. c)Declaration of a local variable of type short
d) Runtime error d) Declaration of a static variable of type String
[Link] the following code within a method, which [Link] class Test
statement is true? {
37
static void operate( StringBuffer x, StringBuffer y) c) The code will compile correctly and will display 10
{ when run.
[Link](y); d) The code will compile correctly and will display 20
y = x; when run.
}
public static void main(String[] args) 289.
{ Given:
StringBuffer x = new StringBuffer("Sun"); int index = 2;
StringBuffer y = new StringBuffer("Java"); Boolean[] test = new Boolean[3];
operate(x,y); Boolean foo = test [index];
[Link](x + "," + y); What is the result?
} a) foo has the value of true.
} b) foo has the value of false.
What is the result? c)foo has the value of null.
a)The code compiles and prints "Sun,Java". d) foo has the value of 0.
b) The code compiles and prints "Sun,Sun".
The code compiles and prints "Java,Java". Topic: String Concepts
b) The code compiles and prints "SunJava,java".
c)The code compiles and prints "SunJava,SunJava". [Link] function does the trim() method of the
d)None of the above String class perform?
a) It returns a string where the leading white space of
[Link] class Test1 the original string has been removed.
{ b) It returns a string where the trailing white space of
private float f1 = 1.0f; the original string has been removed.
float getFloat(){ return f1;} c) It returns a string where both the leading and
public static void main(String[] args) trailing white space of the original string has been
{ removed.
String foo = "ABCDE"; d) It returns a string where all the white space of the
[Link](3); original string has been removed.
[Link]("XYZ");
[Link](foo); [Link] one of the following operators cannot be
} used in conjunction with a String object?
} a) +
What will be the output? b) -
a) Compilation error in the line where "substring" is c) +=
invoked d) .
b) ABXYZ
c) ABCXYZ [Link] method is not defined in the StringBuffer
d)ABCDE class?
a) trim()
[Link] will be the result of attempting to compile b) length()
and run the following class? c) append(String)
public class Assignment { d) reverse()
public static void main(String[] args) {
int a, b, c; [Link] method is not defined in the String class?
b = 10; a) reverse()
a = b = c = 20; b) length()
[Link](a); c) concat(String)
} d)hashCode()
}
a) The code will fail to compile, since the compiler will [Link] statement concerning the charAt() method
recognize that the variable c in the assignment of the String class is true?
statement a = b = c = 20; has not been initialized. a) The index of the first character is 1.
b) The code will fail to compile because the b) The charAt() method returns a Character object.
assignment statement a = b = c = 20; is illegal. c) The expression "abcdef".charAt(3) is illegal.
38
d) expression "abcdef".charAt(3) evaluates to the
character ‘d’. [Link] the following,
[Link] expression will evaluate to true? [Link] JAR files are packaged using the following
a) "Hello there".toLowerCase().equals("hello there") format
b) "HELLO THERE".equals("hello there") a) TAR
c) ("hello".concat("there")).equals("hello there") b) ZIP
d) "Hello There".compareTo("hello there") == 0 c) ARJ
d) CAB
[Link] the following code snippet,
4. String d = "bookkeeper"; [Link] order to run a jar file, say "[Link]" using the
5. [Link](1,7); command "java -jar [Link]", what condition should
6. d = "w" + d; be satisfied?
7. [Link]("woo"); a) [Link] should be given executable permission
8. [Link](d); b) The manifest file of the jar should specify the class
What is the result? Assume, the code given above is a whose main method should be executed.
portion of the code present in a method. c) "-jar" is an invalid option for java command and an
a) wookkeewoo error will be displayed.
b) wbookkeewoo d) There should be a class "[Link]" with the same
c) Compilation fails. name as the jar file for the command to work.
d) An exception is thrown at runtime.
41
[Link] one of the following is not a valid header a) JPG
in the manifest of jar file? b) PNG
a) Specification-Title c) TIF
b)Application-Version d) JAR
c) Implementation-Vendor
d) Name [Link] is the manifest header that is used to
specify the application’s entry point in a JAR file?
321.A special file which is present inside the JAR that a) Class-Path
contain information about the files packaged in a JAR b) Entry-Class
file is known as c) Start-Class
a) Metafest d)Main-Class
b) Metadata
c) Manifest [Link] a class named App1 is located in the
d) Manidata [Link] package. You have compiled the
class. How do you execute the class?
[Link] decide that you wish to add your a) java App1
application’s class to a group of classes that are stored b) java [Link].App1
in the location /examples/basics. c) javac [Link].App1
Complete the code to do this d) java [Link]
a) package [Link];
b) import [Link]; [Link] is the main() method special in a Java
c) import package [Link]; program?
d) package examples/basics; a) It is where the Java interpreter starts whole
program running.
[Link] want the code in [Link] to access the b) Only the main() method may create objects.
[Link] class which is stored within c) Every class must have a main() method.
the [Link] file in the directory /jars. How would d) main() method must be the only static method in a
you compile your code? program.
a) javac -classpath /jars/[Link] [Link]
b) javac -classpath /jars/example [Link] [Link] the following code:
c) javac -classpath /jars/ [Link] public class Test {
d) javac -classpath /jars [Link] public static void main(String[] args)
{
[Link] you are creating a class named Button [Link](args[0]);
that you want to include in a group of related classes }
called controls. }
Identify the correct code that includes the class in that If the above code is compiled and run as follows
group. java Test Hello 1 2 3
a) package controls; What would be the output ?
b) public class Button a) java
c) package Button; b) Test
d) import controls; c) Hello
d) Hello 1 2 3
[Link] is true about the package statement in
Java? Topic: Command Line, System Properties
a) It can appear anywhere in the file as long as the
syntax is correct. [Link] the below mentioned code
b) It should appear after all the import statements but and the command-line invocation as,
before the class declaration.
c) There can be more than one package statement. java CommandArgsThree 1 2 3
d)It should be the first non-comment line in the Java public class CommandArgsThree {
source file. public static void main(String [] args) {
String [][] argCopy = new String[2][2];
[Link] is a file format which enables to bundle int x;
multiple files into a single file argCopy[0] = args;
42
x = argCopy[0].length; d) An attempt to run B from the command line fails.
for (int y = 0; y < x; y++) {
[Link](" " + argCopy[0][y]);
} [Link] the below mentioned code
} and the command-line invocation as,
} java CommandArgs 1 2 3 4
What is the result? 1. public class CommandArgs {
a) 0 0 2. public static void main(String [] args) {
b) 1 2 3. String s1 = args[1];
c) 0 0 0 4. String s2 = args[2];
d) 1 2 3 5. String s3 = args[3];
6. String s4 = args[4];
[Link] the below mentioned code 7. [Link](" args[2] = " + s2);
and the command-line invocation as, 8. }
java CommandArgsTwo 1 2 3 9. }
1. public class CommandArgsTwo { What is the result?
2. public static void main(String [] argh) { a) args[2] = 2
3. String [] args; b) args[2] = 3
4. int x; c) args[2] = 1
5. x = [Link]; d) An exception is thrown at runtime
6. for (int y = 1; y <= x; y++) {
7. [Link](" " + argh[y]); [Link] the following code:
8. } public class Foo {
9. } public static void main(String[] args)
10. } {
What is the result? [Link](args[1]);
a) 0 1 2 }
b) 1 2 3 }
c) 0 0 0 If the above code is compiled and run as follows
d) An exception is thrown at runtime java Foo Apples 9 8 7
What would be the output ?
[Link] the following code: a) java
public class Test { b) Foo
public static void main(String[] args) c) Apples
{ d) 9
[Link]([Link]);
} [Link] the below mentioned code
} and the command-line invocation as,
If the above code is compiled and run as follows java CommandArgsFour 9 6 3
java Test Hello 1 2 3
What would be the output ? public class CommandArgsFour {
a) 6 public static void main(String [] argh) {
b) 5 String [] args;
c) 4 int a;
[Link] [Link] contains a = [Link];
class A {public static void main(String... args) {}} // 1 for (int b= 1; b < a; b++) {
and [Link] contains [Link](" " + argh[b]);
class B {protected static void main(String[] args) {}} // }
2 }
What is the result of attempting to compile each of }
the two class declarations and invoke each main What is the result?
method from the command line? a) null null
a) Compile-time error at line 1. b) 9 6
b) Compile-time error at line 2. c) 6 3
c) An attempt to run A from the command line fails. d) An exception is thrown at runtime
43
b)WiproStyle is a static analysis tool
[Link] the below mentioned code c) WiproStyle is a structural analysis tool
and the command-line invocation as, d) WiproStyle is a testing tool
java CommandArgsFive 9 8 7 6
public class CommandArgsFive { [Link] of the following refers to the analysis of
public static void main(String [] args) { computer software that is performed without actually
Integer i1 = new Integer(args[1]); executing programs?
Integer i2 = new Integer(args[2]); a) runtime analysis
Integer i3 = new Integer(args[3]); b) static analysis
Integer i4 = new Integer(args[4]); c) profiling
[Link](" args[3] = " + i3); d) none of the above
}
} [Link] are coding standards?
What is the result?
a) args[3] = 8 a) Standards to avoid code construct having high
b) args[3] = 7 probability of resulting in an error.
c) args[3] = null b) Standards to be followed during System testing.
d) An exception is thrown at runtime c) Stdards used for defining designing guidelines for
the system.
Topic: WiproStyle d) Standards that cannot be followed during the CUT
phase
[Link] does 'Avoid magic numbers' rule in
WiproStyle throw a violation? [Link] of the WiproStyle rule is violated in below
a) Integer variable is declared snippet of code,
b) A numeric literal that is not defined as a constant is public class Sample{
detected public int method1() {
c) When the integer variable is made global int a =10; int b=20;
d) No such rule in WiproStyle int c = a*b;
return c;
[Link] of the following are advantages of using }
WiproStyle for code review? }
a) Reduces code review effort a) Minimize the number of lines by joining multiple
b) Code is generated automatically shorter lines
c) Code can be reverse engineered b) Avoid return statements
d) All the above c) Declare all variables in a single line
d) Avoid multiple variable declaration in single line
[Link] of the following can be used to automate
code review in Java? [Link] of the following is a benefit of using static
a) Junit analyzer?
b) Jprofiler a) Non-Compliance to coding guidelines can be
c)WiproStyle detected automatically.
d) None of the above b) Unit testing can be performed
c) Code coverage can be measured
[Link] of the following is correct with respect to d) can reverse engineer the code
severity level information in Static Analyzers?
a) Severity levels information helps to fix only the [Link] is the earliest phase in which Wiprostyle
violations with critical severity can be used?
b) Severity levels information helps to ignore the a) System testing
violations with minor severity b) Design
c) Severity levels information helps in better c) Requirements
prioritization of violations d) Coding
d) All of the above
[Link] of the WiproStyle error category is violated
[Link] is WiproStyle? in below snippet of code,
a) WiproStyle is a unit testing tool class Foo{
44
public void testA () { }
[Link]("Entering test");//VIOLATION a) Avoid Nested Blocks
} b) Use arraylist instead of vector
} c) Missing Switch Default
a) Maintainability d) Multiple variable declaration on the same line
b) Security
c) Reliability [Link] of the following violations is thrown by
d) Efficiency WiproStyle in below code section?
class A{
int x, y, z;
[Link] of the WiproStyle error category is violated String firstName, LastName;
if we use tab character in our source code? int myAge, mySize, numShoes = 28;
a)Maintainability int a = 4, b = 5, c = 6;
b) Efficiency }
c) Reliability a) Avoid Nested Blocks
d) Portability b) Multiple variable declaration on the same line
c) Empty Block
[Link] of the following rules does WiproStyle d) Missing Switch Default
handle?
a) Rules to detect code coverage [Link] of the following violations is thrown by
b) Formatting ,naming conventions, java doc WiproStyle in below code section?
c) Rules to detect failed test cases public class SampleViolation{
d) None of the above
protected void finalize () throws Throwable { //
[Link] of the software code quality attribute can VIOLATION
be improved by following consistent formatting }
standard? }
a) Security a) Empty Block
b)Maintainability b) Avoid Nested Blocks
c) Efficiency c) Use SuperFinalize()
d) Formatting related standards do not improve any d) Missing Switch Default
code quality attributes
[Link] of the following violations is thrown by [Link] of the following options should be used to
WiproStyle in below code section? correct the violation on line 9?
public class SrrayListExample { [Link] Foo {
int method(int a, int b) { 2. void bar() {
int i = a + b; [Link]
return i; 4.{
} 5. compressThumbnailToDisk(metadata, image);
} 6.}
a) Use arraylist instead of vector [Link] (IOException e)
b) Class should define a constructor 8.{
c) Avoid instantiating string objects 9. [Link](); //Violation
d) Unused import 10. throw new ResourceError([Link]());
11.}
[Link] of the following violations is thrown by a) [Link]()
WiproStyle in below code section? b) java doc
public class Foo { c) [Link]
public void bar() { d) logger
int x = 2;
switch (x) { [Link] of the following violations is thrown by
case 2: WiproStyle in below code section?
int j = 8; public class SampleViolation {
} public int publicVariable; // VIOLATION
} protected int protectedVariable; // VIOLATION
45
int packageVariable; // VIOLATION a) Simple Statements - line with more than a single
} statement
a) Trailing Array Comma b) Avoid chaining assignment operators
b) Visibility Modifier c) Trailing Array Comma
c) SuperFinalize d) Avoid assignments in operands
d) Missing Switch Default
[Link] int convert(String s) {
[Link] of the following violations is thrown by int i, i2;
WiproStyle for below code section? i = [Link](s).intValue(); // Violation
import java.*; i2 = [Link](i).intValue(); // Violation
import [Link].*; return i2;
import [Link]; }
public void Helllo{ What is the cause of the violation in the above code ,
} that wiprostyle may throw.
a) Use only Star (Demand) Imports a) Do not add empty strings
b) Trailing Array Comma b) Consider replacing this Vector with the newer
c) Avoid Star (Demand) Imports [Link]
d) Avoid multiple import statements c) Unneccessary Wrapper Object creation
d) Avoid instantiating String objects; this is usually
[Link] of the following violations is thrown by unnecessary
WiproStyle in below code section?
public interface Foo { [Link] class Foo {
public void method (); // VIOLATION public void bar() {
abstract int getSize (); // VIOLATION try {
static int SIZE = 100; // VIOLATION // do something
} } catch (Throwable th) { //violation
a) Redundant Modifier [Link]();
b) Trailing Array Comma }
c) Avoid Star (Demand) Imports }
d) SuperFinalize }
a) Avoid using exceptions as flow control
361."Explicitly invalidate Session when user logs off" . b) Avoid catching NullPointerException; consider
This rule address removing the cause of the NPE
a) Java secure coding c)Avoid throwing raw exception types
b) Concurrency and Timing problems d) A catch statement should never catch throwable
c) Data handling problems since it includes errors
d) Logical problems
websession [Link] class InvokeWait {
public void method () throws InterruptedException
[Link] of the following violations will be thrown {
on the given code snippet. wait (); // VIOLATION
class Foo { boolean bar(String a, String b) { return a } What is the cause of the above violation.
== b; }} a) Avoid using exceptions as flow control
a) Do not instantiate a StringBuffer with a char b) Avoid throwing raw exception types
b) Use equals() to compare object references c) Do not implement 'SingleThreadModel' interface
c) Avoid chaining assignment operators d) Call wait() inside while or do-while
d) Always initialize static fields
[Link] class Test {
[Link] violation is expected to be thrown by public static void main() { // VIOLATION
wiprostyle on the below code ? }
public class Test { public void test() {
int method (int a, int b) { }
int i = a + b; return i; // Violation public void test1() {
} }
}
46
What may be the possible coding standard violation 371."The ability of a software product to keep
in the above snippet operating over time without failures that renders the
a) Placement of Constants system unusable" is called ( as per ISO 9126)
b) Avoid Multple overloaded methods a) Portability
c) Place Main method as last method b) Maintainability
d) Use Chain Constructors c) Reliability
d) Efficiency
[Link] class Test {
int AGE; // Violation 372."The aptitude of the source code to undergo
public void method1() { repair and evolution". Is called ( as per ISO 9126)
int AGE; a) Efficiency
} b) Reliability
String NA__ME11; // Violation c) portability
} What is the java coding standard violation d) Maintainability
expected in the code snippet above?
a) Reduntant Modifiers [Link] of code intended to find and fix
b) Declare fields with uppercase character names as mistakes overlooked in the initial development phase.
'final' a) Profiling
c) Avoid unused private fields b) unit testing
d) Always initialize static fields c) defect tracking
[Link] class MI { d) code review
public String[] getNames() {
String[] names = {"ashik","hema"}; [Link] is the ideal time for starting the usage of
if([Link] != 0) { static analyzers
return names; a) as soon as the coding starts.
} else { b) once all the coding is over
return null;//Violation c) along with system testing
} d) after unit testing
}
} [Link] is the recommended procedure for usage of
How can the above highlighted coding standard static analyzers if you have legacy code ? (existing
violation be fixed? code base)
a) Return Zero length array instead of null a) Static analyzer should be run on the legacy code as
b) Avoid return statements well
c) Do not add empty strings b) No need to run static analyzer on Legacy code base.
d) Avoid instantiating String objects; this is usually c) static analyzer usage is not reccomended in this
unnecessary scenario
d) Static analyzers are supposed to be run on the
[Link] abstract class Sample { //VIOLATION newly developed LOCs by you.
public abstract StringBuffer getText();
public abstract int getStartPosition(); [Link] capability of the software product to avoid
public abstract int getEndPosition(); unexpected effects from modifications of the
public abstract int getStartLine(); software. (ISO 9126) is termed as
public abstract int getEndLine(); a) adaptability
} b) portability
What may be the violation thrown by a static c) testability
analyzer at the highlighted line. d) stability
a) If a class Extends / Implements other class then it
should have a Naming Convention as defined by the [Link] class Foo {
user void bar(int a) {
b) anonymous classes used as interface implementors switch (a) {
c) Redeclare non-functional class as interface case 1:
d) Avoid multiple Class or Interface // do something
break;
mylabel: // Violation
47
break; [Link] Foo {
default: boolean bar(String x) {
break; return [Link]("2"); // Violation
} }
} }
} What is the cause of above violation?
What may be the cause of the above violation? a) Unneccessary Wrapper Object creation
a) The default label should be the last label in a switch b) Position literals first in String comparisons
statement c) Avoid instantiating String objects; this is usually
b)A non-case label was present in a switch statement unnecessary
c) Case with no break d) Do not instantiate a StringBuffer with a char
d) Non-static initializers are confusing
[Link] class Foo {
[Link] class Foo { Object bar;
public void bar() { // bar is data or an action or both?
int x = 2; void bar() { //Violation
x = x; //Violation }
} }
} Reason for the violation at the highlighted line in the
What is the java coding standard violation that may code snippet may be due to
be thrown on the above code at the highlighted line? a) The field name indicates a constant but its
a) Possible unsafe assignment to a non-final static modifiers do not
field in a constructor b) It is somewhat confusing to have a field name
b) Unused Local Variable matching the declaring class name
c) Consider simply returning the value vs storing it in c) It is somewhat confusing to have a field name with
local variable ''{0}'' the same name as a method
d) Avoid idempotent operations (like assigning a d) Non-static initializers are confusing
variable to itself)
[Link] class Foo extends Bar {
[Link] class Foo { int foo; //Violation
void bad() { }
List foo = getList(); Reason for the violation at the highlighted line in the
if ([Link]() == 0) {//Violation code snippet may be due to
// blah a) It is somewhat confusing to have a field name
} matching the declaring class name
} b) It is somewhat confusing to have a field name with
How the above violation be fixed regarding collection? the same name as a method
a) Perhaps ''{0}'' could be replaced by a local variable c) The field name indicates a constant but its
b)Position literals first in String comparisons modifiers do not
c) Substitute calls to size() == 0 (or size() != 0) with d)Non-static initializers are confusing
calls to isEmpty()
d) Avoid instantiating String objects; this is usually [Link] Phase in which code review tools / static
unnecessary analyzers are supposed to be used for best results
a) CUT phase
[Link] capability of the software product to protect b) System Testing
information and data so that unauthorized persons or c) Design
systems cannot read or modify them and authorized d) Integration Testing
persons or systems are not denied access to them is
termed as [Link] class SampleViolation {
a) Security public copyArray (int[] array) {
b) Efficiency int k =0;
c) Stability int length = [Link];
d) Usability Compliance int[] copy = new int [length];
for(int i = 1; i < length;i++) {
copy[i] = array[i]; // VIOLATION
48
} a) Above statement is correct only in case of large
while(k < length){ applications
copy[k] = array[k++]; // VIOLATION b) Above statement is correct only in case of small
} applications
} c) Above statement is correct in case of all
} applications
What is the reccomended procedure to fix the above d) Above statement is NOT correct in case of all
violations thrown on coping two arrays applications
a) Instead of copying data between two arrays, use
[Link] method which is efficient. [Link] is unit testing?
b) Do not add empty arrays a) Testing each unit of code in an isolation
c) Trailing Array Comma b) Testing code linewise
d) Avoid arraylength in loops c) Testing individual class of code in an isolation
d) None of the above
386.A form of static analysis based on the definition
and usage of variables [Link] is the purpose of Data Driven Test (DDT)
a) Profiling testing feature?
b) Data Flow Analysis a)editing of tests to change values in tool generated
c) peer review test cases
d) coverage analysis b) generation of more number of test so that method
can be tested with all possible values
[Link] Foo { void bar(Object x) { if (x != null && x c) Customization of test classes. It allows users to add
instanceof Bar)// Violation. any number of test classes
What may be the cause of the violation? d) Parameterization of tests with user defined test
a) Reduntant Modifiers data
b) Avoid chaining assignment operators
c) No need to check for null before an instanceof [Link] is the basic intention of performing unit
d) Avoid assignments in operands testing?
a) to avoid system testing
Topic: WUT b) to avoid system functionality testing
c) to detect problems early in the development stage
[Link] done unit testing can replace d) to avoid regression testing
system testing. Check the correctness
a) Yes, unit testing can replace system testing in all [Link] of the following is given highest priority
cases while fixing unit testing problems ?
b) Yes, unit testing can replace sys testing only if it is a) Assertion failures
tool based b) Exceptions
c) Yes, unit testing can replace sys testing only if it is c) Timeout errors
JUnit based testing d) No prioritization is required
d) No, unit testing can NOT replace system testing
[Link] is Code coverage analysis?
[Link] the correct statement related to unit a) Process of finding areas of a program NOT
testing exercised by a set of test cases
a) Systematically done unit testing can replace system b) Process of finding failed test cases
testing c) Process of finding areas of programs throwing
d) If code reviews & code inspections are done errors
thoroughly unit testing is NOT required d) Process of finding areas of program NOT exercised
b) Both Unit testing and System testing are required because of exceptions
as they compliment each other
c) In any case either system testing or unit testing is [Link] of the below statements is correct
required; but NOT the both regarding Unit testing?
a) Unit tests can be thrown away once the code is
[Link] testing is required even if code reviews & tested
code inspections are done thoroughly. Check the b) Unit testing is NOT required if system testing is
correctness done with effectiveness
49
c) Unit testing and System testing compliment each b) When a method is declared as "protected", it can
other only be accessed within the same package where the
d) Unit testing is required only in projects using Agile class is [Link] order to test a "protected" method
development process of a target class, you need to define your test class in
the same package as the target class.
[Link] is considered as fundamental unit of
coverage?
a) Type coverage c) When a method is declared as "protected", it can
b) Block coverage only be accessed within the same package where the
c) Package coverage class is defined we can write a test case inside target
d) Test coverage class.
d) None of the above
[Link] does calculating and tracking of metrics
help? [Link] are the benefits of Unit Testing?
a) Helps in reducing static analysis effort a) The modular approach during Unit testing
b) Helps to identify some of the symptoms of poor eliminates the dependency on other modules during
design testing.
c) Helps to avoid unit testing b) We can test parts of a project with out waiting for
d) None of the above the other parts to be available.
c) Designers can identify and fix problem immediately,
[Link] the code coverage for below code. as the modules are best known to them. This helps in
public void testAdd1() throws Throwable { fixing multiple problems simultaneously
int actual1 = [Link](338,18); d) All of the above
assertEquals(356, actual1);
int actual2 = [Link](36, 39); [Link] of the following statement is wrong about
assertEquals(75, actual2); unit testing
int actual3 = [Link](100, 8); a) Integration Test is a replacement of Unit testing
assertEquals(108, actual3); which will Catch all the Bugs Anyway.
} b) Cost of fixing a defect identified during the early
a) Full Coverage stages is less compared to that during later stage.
b) Partial coverage c) We can test parts of a project with out waiting for
c) Not Covered the other parts to be available
d) None of the above d) Designers can identify and fix problem
immediately, as the modules are best known to them.
[Link] of the following statement is correct with This helps in fixing multiple problems simultaneously
respect to private method in Unit Testing ?
a) Private methods can't be tested during unit testing [Link] is meant by Code Coverage in Unit Testing ?
b) When a method is declared as "private", it can only a) A code coverage tool simply keeps track of which
be accessed within the same class. So there is no way parts of your code get executed and which parts do
to test a "private" method of a target class from any not.
test class. So we can write a test case inside target b) A code coverage tool simply keeps track of pass and
class failure scenario of test cases.
c) When a method is declared as "private", it can only c) A code coverage tool simply keeps track of which
be accessed within the same class. So there is no way parts of your code has private and protected method.
to test a "private" method of a target class from any d) None of the above
test class. You have to perform unit testing manually.
Or you have to change your method from "private" to [Link] a Unit testing framework will be helpful for
"protected". Unit Testing
d) None of the above a) It helps to skip unit testing and do functional testing
directly so as to reduce effort
[Link] of the following statement is correct with b)It helps to simplify the process of unit testing by
respect to protected method ? reusable set of libraries or classes that have been
a) Protected methods can not be tested during unit developed for a wide variety of languages
testing c) which helps to test values with boundary conditions
d) None of the above
50
a) public void testDivide1() throws Throwable { int
[Link] is Data Driven Testing in Unit Testing ? actual1 = [Link](1, -2147483648);
a) It is a test approach to test private method in the assertEquals(1, actual1); int actual2 =
class [Link](-2147483648, 1); assertEquals(1,
b) It is single test to verify many different test cases by actual2); }
driving the test with input and expected values from b) Test case can't be written since it is static method
an external data source c) Test case can't be written since it is public method
c) It is a test approach to test protected method in the d) None of the above
class
d) It is a test approach to test values with boundary [Link] a test case can be written for this method ?
conditions public static boolean startsWith(String str,String
match){
[Link] of the below statements are true about for (int i= 0; i < [Link](); ++i) {
Data Driven Testing in Unit Test? if([Link](i)!= [Link](i))
1) all input data and expected results for your return false;
automated tests are kept in one place, which makes it }
easier to maintain test cases return true;
2)you can also execute expressions specified in cells of }
the processed storage (for example, your storage can a) public void testStartsWith1() throws Throwable
contain the value of 5+5) { boolean actual1 =
3)After first failure test case remaining test cases will [Link]("853956.85395645", "d R0");
not be executed assertEquals(false, actual1); boolean actual2 =
a) Both 1 & 2 [Link]("853956.85395645", (String)
b) Both 1 & 3 null); assertEquals(true, actual2); }
c) Both 2 & 3
d) All three statements b) public void testStartsWith1() throws Throwable
{ boolean actual1 =
[Link] to write a test case for the method add in [Link]("853956.85395645", "d R0");
the below class Sample. assertNotNull(false, actual1); boolean actual2 =
[Link]("853956.85395645", (String)
a) public class Sample { private int addInteger(int i, null); assertNotNull(true, actual2); }
int j){ int sum; sum=i+j; return sum; } }
b) Private methods can't be tested during unit testing c)Test case can't be written since it is static method
Test case can be written inside target class itself d) Test case can't be written since it is public method
c) Unit testing needs to be done either manually or
test case can be written by changing access modifier [Link] class Student { public void setAge(int age) {
"private" to "protected" [Link] = age;
d) None of the above }}
How the case can be written for the above bean class
[Link] int addInteger(int i, int j){ int sum; method?
sum=i+j; return sum; } a) No need to write a test case for bean class
How a test case can be written for this method? methods
a) Protected methods can not be tested during unit b) public void testSetAge1() throws Throwable
testing { Student student = new Student(); [Link](0);
b) Test case can be written by defining the test class in [Link](1); [Link](-1);
the same package as the target class. [Link](2147483647); [Link](-
c) Since protected methods can't be accessed outside 2147483648); }
the package unit testing needs to be done either c) Bean class methods can not be tested during unit
manually or test case can be written by changing testing
access modifier "protected" to "public" d) None of the above
d) None of the above
[Link] class TestDb {
[Link] static int Divide (int i1, int i2) { return public String readABC(Connection c,String
i1/i2; } table_name) throws SQLException{
How a test case can be written for this method ? Statement stm=[Link]();
51
ResultSet rs=[Link]("select a a) Test case is not required as there is no functionality
from"+table_name); in this method affected by external calls
int a=0; b) Stubs can be used to write test cases
a=[Link]("a"); c) Data Driven Testing can be used to write test cases
String result; d) None of the above
result =" result "+ a ;
return result; [Link] of the following is a framework for Java
} Unit testing ?
} 1 JUnit 2 GUnit 3 NUnit 4 Unit++
How test case can be written for the above method?
a) Test case can't be written since it has Connection [Link] identify Java Unit testing tools
object as a parameter 1) JDeveloper 2) JTest 3) WiproUT 4)JUnit
b) Object mocking can be used to write test cases
c) Data Driven Testing can be used to write test cases 2,3,4
d) None of the above 1,2,4
All 1,2,3 &4
[Link] class ConstructorExample { Only 4
public static long getFileLength (String path)
throws IOException { 419.
RandomAccessFile file = new RandomAccessFile public static int Divide (int i1, int i2) { return i1/i2; }
(path, "rw"); Please examine the below test case for the above
return [Link] (); method.
} public void testDivide1() throws Throwable {
} int actual1 = [Link](16,8);
How a test case can be written for this method ? assertEquals(2, actual1);
a) Test case can't be written for this method int actual2 = [Link](18, 1);
b) Stubs can be used to write test cases assertEquals(1, actual2); }
c) Data Driven Testing can be used to write test cases a) Given test case won't be executed since test case
d) None of the above can't be written for static method
b) First assert statement will be passed and second
[Link] static List getScores(String team_name) assert will be failed
throws SQLException { c) Both assert statement will be passed
_loggedCalls.add("getScores: " + team_name); d)Test case is not required for this method
prepare();
List list_scores = new ArrayList(); [Link], at what stage in the SDLC cycle Unit
Statement stmt = _connection.createStatement(); Testing tool is applicable?
ResultSet rs = stmt 1 CUT phase 2 Testing phase 3 Design phase
.executeQuery("SELECT * FROM SCORES WHERE 4 UAT phase
TEAM_NAME='"
+ team_name + "'"); [Link], Unit Testing tool is supposed to be used
while ([Link]()) { by ___________
int score = [Link]("SCORE"); 1 only Project Managers 2 All Developers 3 only
list_scores.add(new Integer(score)); Test Engineers 4 only Quality Analyst
}
return list_scores; [Link] the correct statement related to Unit
} Testing tool
How a test case can be written for this method ? It is a system functionality and regression testing tool
a) Test case can't be written for this method It is a system level control flow testing tool
b) Stubs can be used to write test cases It is a unit level black-box and white-box testing tool
c) Data Driven Testing can be used to write test cases It is a system level black-box and white-box testing
d) None of the above tool
The code contains a compilation error. The bitwise XOR operator (^) returns an integer value and cannot be directly used in an if statement as a boolean without comparison, causing a type mismatch. Therefore, option 'c' is correct since 'Compilation error occurs' .
The code will output 'Finally'. The finally block is executed even after the return statement in the try block .
Unit testing frameworks simplify the process by providing reusable libraries and support for various languages, facilitating consistent and efficient testing practices. They streamline the creation, execution, and verification of test cases .
Unit testing is essential because it facilitates early detection of issues, offers insights through modular testing without waiting for system availability, and helps identify specific areas of bugs, all of which code reviews and inspections might not reveal comprehensively. This remains true across all applications .
The code results in a compile-time error because 'while(i)' expects a boolean condition but is given an integer instead. Java does not inherently convert integers to boolean, so 'while(i)' is invalid .
The program will print 'Z'. The conditional statement 'if (bFlag = false)' assigns false to bFlag rather than comparing it, leading to else if check being false and the final else block gets executed .
The output of the code is '20'. The parameter x in method() hides the instance variable x. The operation 'x+=x;' doubles the value of the method's parameter, resulting in 20 when 10 is passed to method().
The output will be '6,6'. The XOR operator ^ between 2 (binary 0010) and 4 (binary 0100) results in 6 (binary 0110). Since XOR is commutative, (y ^ x) is the same as (x ^ y), both yielding 6 .
If run with no arguments, it will print only 'The end'. The absence of arguments makes args.length zero, so return is executed before attempting to print args[0], skipping the output of any argument but still executing finally .
Code coverage is crucial for unit testing as it ensures that all parts of the program are exercised by test cases, highlighting sections of the code that have not been tested, thus ensuring thorough validation and reducing potential areas of uncovered bugs .