Questions and Answers.

Similar documents
JAVA AS OBJECT ORIENTED PROGRAMMING LANGUAGE

1.Which four options describe the correct default values for array elements of the types indicated?

1. Short circuit AND (&&) = if first condition is false then the second is never evaluated

1 Shyam sir JAVA Notes

Prof. Navrati Saxena TA: Rochak Sachan

SECTION II: LANGUAGE BASICS

JAVA OPERATORS GENERAL

Chapter 3: Operators, Expressions and Type Conversion

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Operators and Expressions

CSC 1214: Object-Oriented Programming

Operators Questions

Introduction to Programming Using Java (98-388)

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

3 Operators and Assignments

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

CS260 Intro to Java & Android 03.Java Language Basics

The Java language has a wide variety of modifiers, including the following:

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

A Short Summary of Javali

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Lexical Considerations

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

STRUCTURING OF PROGRAM

Points To Remember for SCJP

Lexical Considerations

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Zheng-Liang Lu Java Programming 45 / 79

Operators. Java Primer Operators-1 Scott MacKenzie = 2. (b) (a)

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Program Fundamentals

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA

Operators. Java operators are classified into three categories:

Index COPYRIGHTED MATERIAL

1 Lexical Considerations

The SPL Programming Language Reference Manual

Exercises Software Development I. 05 Conversions and Promotions; Lifetime, Scope, Shadowing. November 5th, 2014

CS 231 Data Structures and Algorithms, Fall 2016

Lecture Set 4: More About Methods and More About Operators

Declarations and Access Control SCJP tips

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

UNIT- 3 Introduction to C++

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

COP 3330 Final Exam Review

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Object-Oriented Programming

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

3. Java - Language Constructs I

public class Test { static int age; public static void main (String args []) { age = age + 1; System.out.println("The age is " + age); }

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Java Bytecode (binary file)

IC Language Specification

The PCAT Programming Language Reference Manual

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

Basics of Java Programming

Answer1. Features of Java

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Java Programming Language. 0 A history

Lecture Set 4: More About Methods and More About Operators

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

The Decaf Language. 1 Lexical considerations

Java Primer 1: Types, Classes and Operators

Operators. 3 Two-Minute Drill CERTIFICATION OBJECTIVES Q&A. Using Operators. Self Test

VARIABLES AND TYPES CITS1001

Primitive Data, Variables, and Expressions; Simple Conditional Execution

Expressions & Flow Control

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Java Programming. Atul Prakash

Building Java Programs Chapter 2

CMPT 125: Lecture 3 Data and Expressions

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Chapter 2: Using Data

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Array. Prepared By - Rifat Shahriyar

More Programming Constructs -- Introduction

CS313D: ADVANCED PROGRAMMING LANGUAGE

Unit 3. Operators. School of Science and Technology INTRODUCTION

Computer Components. Software{ User Programs. Operating System. Hardware

Lara Technologies Special-Six Test

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Language Fundamentals Summary

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Full file at

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Variables and literals

The Decaf language 1

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Building Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw.

Building Java Programs Chapter 2

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Transcription:

Q.1) What is the output of this program? classmainclass char a = 'A'; a++; System.out.print((int)a); A. 66 B. 67 C. 65 D. 64 ANSWER : 66 ASCII value of 'A' is 65, on using ++ operator character value increments by one. output: $ javac mainclass.java $ javamainclass 66 Q.2) What will be the output of the program? public class CommandArgs public static void main(string [] args) String s1 = args[1]; String s2 = args[2]; String s3 = args[3]; String s4 = args[4]; System.out.print(" args[2] = " + s2); and the command-line invocation is >javacommandargs 1 2 3 4 A. args[2] = 2 B. args[2] = 3 C. args[2] = null D. An exception is thrown at runtime. ANSWER : An exception is thrown at runtime. An exception is thrown because in the code String s4 = args[4];, the array index (the fifth element) is out of bounds. The exception thrown is the cleverly named ArrayIndexOutOfBoundsException. Q.3) Which three are valid declarations of a float? float f1 = -343; float f2 = 3.14; float f3 = 0x12345; float f4 = 42e7; float f5 = 2001.0D; float f6 = 2.81F; A. 1, 2, 4 B. 2, 3, 5 Page 1

C. 1, 3, 6 D. 2, 4, 6 ANSWER : 1, 3, 6 (1) and (3) are integer literals (32 bits), and integers can be legally assigned to floats (also 32 bits). (6) is correct because (F) is appended to the literal, declaring it as a float rather than a double (the default for floating point literals). (2), (4),and (5) are all doubles. Q.4) What will be the output of the program? public class CommandArgsTwo public static void main(string [] argh) int x; x = argh.length; for (int y = 1; y <= x; y++) System.out.print(" " + argh[y]); and the command-line invocation is >javacommandargstwo 1 2 3 A. 0 1 2 B. 1 2 3 C. 0 0 0 D. An exception is thrown at runtime ANSWER : An exception is thrown at runtime An exception is thrown because at some point in (System.out.print(" " + argh[y]);), the value of x will be equal to y, resulting in an attempt to access an index out of bounds for the array. Remember that you can access only as far as length - 1, so loop logical tests should use x <somearray.length as opposed to x < = somearray.length. Q.5) What is the output of this program? class increment int g = 3; System.out.print(++g * 8); A. 25 B. 24 C. 32 D. 33 ANSWER : 32 Operator ++ has more preference than *, thus g becomes 4 and when multiplied by 8 gives 32. output: $ javac increment.java $ java increment 32 Page 2

Q.6) public class F0091 public void main( String[] args ) System.out.println( "Hello" + args[0] ); What will be the output of the program, if this code is executed with the command line: >java F0091 world A. Hello B. Hello Foo91 C. Hello world D. The code does not run. ANSWER : The code does not run. D is correct. A runtime error will occur owning to the main method of the code fragment not being declared static: Exception in thread "main" java.lang.nosuchmethoderror: main The Java Language Specification clearly states: "The main method must be declared public, static, and void. It must accept a single argument that is an array of strings." Q.7) What is the output of this program? class variable_scope int x; x = 5; int y = 6; System.out.print(x + " " + y); System.out.println(x + " " + y); A. 5 6 5 6 B. 5 6 5 C. Runtime error D. Compilation error ANSWER : Compilation error Second print statement doesn't have access to y, scope y was limited to the block defined after initialization of x. output: $ javac variable_scope.java Exception in thread main java.lang.error: Unresolved compilation problem: y cannot be resolved to a variable Q.8) Which of these coding types is used for data type characters in Java? A. ASCII B. ISO-LATIN-1 C. UNICODE D. None of the mentioned ANSWER : UNICODE Page 3

Explanation: Unicode defines fully international character set that can represent all the characters found in all human languages. Its range is from 0 to 65536. Q.9) Which is a valid keyword in java? A. interface B. string C. Float D. unsigned ANSWER : interface interface is a valid keyword. B is wrong because although "String" is a class type in Java, "string" is not a keyword. C is wrong because "Float" is a class type. The keyword for the Java primitive is float. D is wrong because "unsigned" is a keyword in C/C++ but not in Java.a Q.10) What is the output of this program? class area double r, pi, a; r = 9.8; p1 = 3.14; a = pi * r * r; System.out.println(a); A. 301.5656 B. 301 C. 301.56 D. 301.56560000 ANSWER : 301.5656 output: $ javac area.java $ java area 301.5656 Q.11) What is the output of this program? classdynamic_initialization double a, b; a = 3.0; b = 4.0; double c = Math.sqrt(a * a + b * b); System.out.println(c); A. 5.0 B. 25.0 C. 7.0 D. Compilation Error Page 4

ANSWER : 5.0 Variable c has been dynamically initialized to square root of a * a + b * b, during run time. output: $ javac dynamic_initialization.java $ javadynamic_initialization 5.0 Q.12) Which of these can not be used for a variable name in Java? A. identifier B. keyword C. both a < b D. None of the mentioned ANSWER : keyword Keywords are specially reserved words which can not be used for naming a user defined variable, example : class, int, for etc Q.13) Which is the valid declarations within an interface definition? A. public double methoda(); B. public final double methoda(); C. static void methoda(double d1); D. protected void methoda(double d1); ANSWER : public double methoda(); A is correct. A public access modifier is acceptable. The method prototypes in an interface are all abstract by virtue of their declaration, and should not be declared abstract. B is wrong. The final modifier means that this method cannot be constructed in a subclass. A final method cannot be abstract. C is wrong. static is concerned with the class and not an instance. D is wrong. protected is not permitted when declaring a method of an interface. See information below. Member declarations in an interface disallow the use of some declaration modifiers; you cannot use transient, volatile, or synchronized in a member declaration in an interface. Also, you may not use the private and protected specifiers when declaring members of an interface. Q.14) What is the numerical range of a char in Java? A. -128 to 127 B. 0 to 256 C. 0 to 32767 D. 0 to 65535 ANSWER : 0 to 65535 Char occupies 16-bit in memory, so it supports 2^16 i:e from 0 to 65535. Q.15) Which of these is data type long literal? A. 0x99fffL B. ABCDEFG C. 0x99fffa D. 99671246 Page 5

ANSWER : 0x99fffL Data type long literals are appended by an upper or lowercase L. 0x99fffL is hexadecimal long literal. Q.16) What is the range of data type byte in Java? A. -128 to 127 B. -32768 to 32767 C. -2147483648 to 2147483647 D. None of the above ANSWER : -128 to 127 Byte occupies 8 bits in memory. Its range is from -128 to 127. Q.17) What will be the output of the program? public class TestDogs public static void main(string [] args) Dog [][] thedogs = new Dog[3][]; System.out.println(theDogs[2][0].toString()); class Dog A. null B. thedogs C. Compilation fails D. An exception is thrown at runtime ANSWER : An exception is thrown at runtime The second dimension of the array referenced by thedogs has not been initialized. Attempting to access an uninitialized object element (System.out.println(theDogs[2][0].toString());) raises a NullPointerException. Q.18) What is the range of data type short in Java? A. -128 to 127 B. -32768 to 32767 C. -2147483648 to 2147483647 D. None of the mentioned ANSWER : -32768 to 32767 Short occupies 32 bits in memory. Its range is from -32768 to 32767. Q.19) In the given program, how many lines of output will be produced? 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][]; Page 6

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); A. 7 B. 9 C. 11 D. 13 ANSWER : 11 The loops use the array sizes (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. Q.20) What will be the output of the program? public class Test public static void main(string [] args) signedint x = 10; for (int y=0; y<5; y++, x--) System.out.print(x + ", "); A. 10, 9, 8, 7, 6, B. 9, 8, 7, 6, 5, C. Compilation fails. D. An exception is thrown at runtime. ANSWER : Compilation fails. The word "signed" is not a valid modifier keyword in the Java language. All number primitives in Java are signed. Hence the Compilation will fails. Q.21) What is the output of this program? class main_arguments public static void main(string [] args) Page 7

String [][] argument = new String[2][2]; int x; argument[0] = args; x = argument[0].length; for (int y = 0; y < x; y++) System.out.print(" " + argument[0][y]); A. 1 1 B. 1 0 C. 1 0 3 D. 1 2 3 ANSWER : 1 2 3 In argument[0] = args;, the reference variable arg[0], which was referring to an array with two elements, is reassigned to an array (args) with three elements. Output: $ javac main_arguments.java $ javamain_arguments 1 2 3 Q.22) Which of these literals can be contained in a data type float variable? A. 1.7e-308 B. 3.4e-038 C. 1.7e+308 D. 3.4e-050 ANSWER : 3.4e-038 Range of data type float is 3.4e-038 to 3.4e+308. Q.23) Which of the following are legal lines of Java code? 1. int w = (int)888.8; 2. byte x = (byte)100l; 3. long y = (byte)100; 4. byte z = (byte)100l; A. 1 and 2 B. 2 and 3 C. 3 and 4 D. All statements are correct. ANSWER : All statements are correct. Statements (1), (2), (3), and (4) are correct. (1) is correct because when a floating-point number (a double in this case) is cast to an int, it simply loses the digits after the decimal.(2) and (4) are correct because a long can be cast into a byte. If the long is over 127, it loses its most significant (leftmost) bits.(3) actually works, even though a cast is not necessary, because a long can store a byte. Q.24) What is the output of this program? class evaluate int a[] = 1,2,3,4,5; Page 8

int d[] = a; int sum = 0; for (int j = 0; j < 3; ++j) sum += (a[j] * d[j + 1]) + (a[j + 1] * d[j]); System.out.println(sum); A. 38 B. 39 C. 40 D. 41 ANSWER : 40 output: $ javac evaluate.java $ java evaluate 40 Q.25) What is the output of this program? Class asciicodes char var1 = 'A'; char var2 = 'a'; System.out.println((int)var1 + " " + (int)var2); A. 162 B. 65 97 C. 67 95 D. 66 98 ANSWER : 65 97 ASCII code for 'A' is 65 and for 'a' is 97. output: $ javac asciicodes.java $ javaasciicodes 65 97 Q.26) What is the output of this program? class mainclass boolean var1 = true; boolean var2 = false; if (var1) System.out.println(var1); else System.out.println(var2); A. 0 B. 1 Page 9

C. true D. false ANSWER : true output: $ javac mainclass.java $ javamainclass true Q.27) If an expression contains double, int, float, long, then whole expression will promoted into which of these data types? A. long B. int C. double D. float ANSWER : double If any operand is double the result of expression is double. Q.28) An expression involving byte, int, and literal numbers is promoted to which of these? A. int B. long C. byte D. float ANSWER : int An expression involving bytes, ints, shorts, literal numbers, the entire expression is promoted to int before any calculation is done. Q.29) What is the output of this program? class conversion double a = 295.04; int b = 300; byte c = (byte) a; byte d = (byte) b; System.out.println(c + " " + d); A. 38 43 B. 39 44 C. 295 300 D. 295.04 300 ANSWER : 39 44 Type casting a larger variable into a smaller variable results in modulo of larger variable by range of smaller variable. b contains 300 which is larger than byte's range i:e -128 to 127 hence d contains 300 modulo 256 i:e 44. output: $ javac conversion.java Page 10

$ java conversion 39 44 Q.30) Which of these is returned by operators &,? A. Integer B. Boolean C. Character D. Float ANSWER : Integer Here & is a BITWISE AND it returns integer value. Q.31) Which of these values can a boolean variable contain? A. True & False B. 0 & 1 C. Any integer value. D. Both a & b ANSWER : True & False Boolean variable can contain only one of two possible values, true and false. Q.32) What will be the output of the program? public class CommandArgsThree public static void main(string [] args) String [][] argcopy = new String[2][2]; int x; argcopy[0] = args; x = argcopy[0].length; for (int y = 0; y < x; y++) System.out.print(" " + argcopy[0][y]); and the command-line invocation is >javacommandargsthree 1 2 3 A. 0 0 B. 1 2 C. 0 0 0 D. 1 2 3 ANSWER : 1 2 3 In argcopy[0] = args;, the reference variable argcopy[0], which was referring to an array with two elements, is reassigned to an array (args) with three elements. Q.33) What is the output of this program? class conversion double a = 295.04; int b = 300; byte c = (byte) a; Page 11

byte d = (byte) b; System.out.println(c + " " + d); A. 38 43 B. 39 44 C. 295 300 D. 295.04 300 ANSWER : 39 44 Type casting a larger variable into a smaller variable results in modulo of larger variable by range of smaller variable. b contains 300 which is larger than byte's range i:e -128 to 127 hence d contains 300 modulo 256 i:e 44. output: $ javac conversion.java $ java conversion 39 44 Q.34) What is the output of this program? class array_output int array_variable [] = new int[10]; for (int i = 0; i < 10; ++i) array_variable[i] = i/2; arry_variable[i]++; System.out.print(array_variable[i] + " "); i++; A. 0 2 4 6 8 B. 0 1 2 3 4 C. 0 1 2 3 4 5 6 7 8 9 D. 1 2 3 4 5 6 7 8 9 10 ANSWER : 0 1 2 3 4 When an array is declared using new operator then all of its elements are initialized to 0 automatically. for loop body is executed 5 times as whenever controls comes in the loop i value is incremented twice, first by i++ in body of loop then by ++i in increment condition of for loop. output: $ javac array_output.java $ javaarray_output 0 1 2 3 4 Q.35) Which one is a valid declaration of a boolean? A. boolean b1 = 1; B. boolean b2 = false; C. boolean b3 = false; D. boolean b4 = true; ANSWER : boolean b3 = false; Page 12

Boolean can only be assigned true or false literals. Q.36) What is the output of this program? classarray_output chararray_variable [] = new char[10]; for (int i = 0; i < 10; ++i) array_variable[i] = "i"; System.out.print(array_variable[i] + "" ); i++; A. iiiii B. 0 1 2 3 4 C. i j k l m D. None of the mentioned ANSWER : iiiii $ javac array_output.java $ javaarray_output iiiii Q.37) public interface Foo int k = 4; /* Line 3 */ Which three piece of codes are equivalent to line 3? finalint k = 4; publicint k = 4; staticint k = 4; abstractint k = 4; volatileint k = 4; protectedint k = 4; A. 1, 2 and 3 B. 2, 3 and 4 C. 3, 4 and 5 D. 4, 5 and 6 ANSWER : 1, 2 and 3 (1), (2) and (3) are correct. Interfaces can have constants, which are always implicitly public, static, and final. Interface constant declarations of public, static, and final are al in any combination. Q.38) What is the output of this program? class booloperators boolean var1 = true; boolean var2 = false; System.out.println((var2 & var2)); Page 13

A. 0 B. 1 C. true D. false ANSWER : false boolean '&' operator always returns true or false. var1 is defined true and var2 is defined false hence their '&' operator result is false. output: $ javac booloperators.java $ javabooloperators False Q.39) What is the output of this program? class A final public int calculate(int a, int b) return 1; class B extends A publicint calculate(int a, int b) return 2; public class output B object = new B(); System.out.print("b is " + b.calculate(0, 1)); A. b is : 2 B. b is : 1 C. Compilation Error. D. An exception is thrown at runtime. ANSWER : Compilation Error. The code does not compile because the method calculate() in class A is final and so cannot be overridden by method of class b. Q.40) What is the output of this program? class c public void main( String[] args ) System.out.println( "Hello" + args[0] ); A. Hello c B. Hello C. Hello world D. Runtime Error. ANSWER : Runtime Error. A runtime error will occur owning to the main method of the code fragment not being declared static. Output: $ javac c.java Exception in thread ~Ac^a'notA"main~Ac^a'not^A Page 14

Q.41) Which of these occupy first 0 to 127 in Unicode character set used for characters in Java? A. ASCII B. ISO-LATIN-1 C. None of the mentioned D. Both a & b ANSWER : Both a & b First 0 to 127 character set in Unicode are same as those of ISO-LAIN-1 and ASCII. Q.42) Which will legally declare, construct, and initialize an array? A. int [] mylist = "1", "2", "3"; B. int [] mylist = (5, 8, 2); C. intmylist [] [] = 4,9,7,0; D. intmylist [] = 4, 3, 7; ANSWER : intmylist [] = 4, 3, 7; The only legal array declaration and assignment statement is D A is wrong because it initializes an int array with String literals. B is wrong because it use something other than curly braces for the initialization. C is wrong because it provides initial values for only one dimension, although the declared array is a two-dimensional array. Q.43) What will be the output of the program? 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 A. names B. null C. Compilation fails D. An exception is thrown at runtime ANSWER : null The names array is initialized with five null elements. Then elements 0 and 1 are assigned the String values "a" and "b" respectively (the command-line arguments passed to main). Elements of names array 2, 3, and 4 remain unassigned, so they have a value of null. Q.44) What is the numerical range of a char? A. -128 to 127 B. -(215) to (215) - 1 C. 0 to 32767 D. 0 to 65535 Page 15

ANSWER : 0 to 65535 A char is really a 16-bit integer behind the scenes, so it supports 216 (from 0 to 65535) values. Q.45) import java.awt.*; class Ticker extends Component public static void main (String [] args) Ticker t = new Ticker(); /* Missing Statements? */ which two of the following statements, inserted independently, could legally be inserted into missing section of this code? boolean test = (Component instanceof t); boolean test = (t instanceof Ticker); boolean test = t.instanceof(ticker); boolean test = (t instanceof Component); A. 1 and 4 B. 2 and 3 C. 1 and 3 D. 2 and 4 ANSWER : 2 and 4 (2) is correct because class type Ticker is part of the class hierarchy of t; therefore it is a legal use of the instanceof operator. (4) is also correct because Component is part of the hierarchy of t, because Ticker extends Component. (1) is incorrect because the syntax is wrong. A variable (or null) always appears before the instanceof operator, and a type appears after it. (3) is incorrect because the statement is used as a method (t.instanceof(ticker);), which is illegal. Q.46) Which two statements are equivalent? 1. 3/2 2. 3<2 3. 3*4 4. 3<<2 A. 1 and 2 B. 2 and 3 C. 3 and 4 D. 1 and 4 ANSWER : 3 and 4 (1) is wrong. 3/2 = 1 (integer arithmetic). (2) is wrong. 3 < 2 = false. (3) is correct. 3 * 4 = 12. (4) is correct. 3 <<2= 12. In binary 3 is 11, now shift the bits two places to the left and we get 1100 which is 12 in binary (3*2*2). Q.47) What will be the output of the program? class Test public static void main(string [] args) Page 16

int x=20; String sup = (x < 15)? "small" : (x < 22)? "tiny" : "huge"; System.out.println(sup); A. small B. tiny C. huge D. Compilation fails ANSWER : tiny This is an example of a nested ternary operator. The second evaluation (x < 22) is true, so the "tiny" value is assigned to sup. Q.48) Which of these is returned by greater than, <, and equal to, ==, operator? A. Integers B. Floating - point numbers C. Boolean D. None of the mentioned ANSWER : Boolean All relational operators return a boolean value i:e true and false. Q.49) What is the output of this program? class operators int x = 8; System.out.println(++x * 3 + " " + x); A. 24 8 B. 24 9 C. 27 8 D. 27 9 ANSWER : 27 9 Operator ++ has higher precedence than multiplication operator, *, x is incremented to 9 than multiplied with 3 giving 27. output: $ javac operators.java $ java operators 27 9 Q.50) What will be the output of the program class PassA public static void main(string [] args) PassA p = new PassA(); p.start(); Page 17

void start() long [] a1 = 3,4,5; long [] a2 = fix(a1); System.out.print(a1[0] + a1[1] + a1[2] + " "); System.out.println(a2[0] + a2[1] + a2[2]); long [] fix(long [] a3) a3[1] = 7; return a3; A. 12 15 B. 15 15 C. 3 4 5 3 7 5 D. 3 7 5 3 7 5 ANSWER : 15 15 Output: 15 15 The reference variables a1 and a3 refer to the same long array object. When the [1] element is updated in the fix() method, it is updating the array referred to by a1. The reference variable a2 refers to the same array object. So Output: 3+7+5+" "3+7+5 Output: 15 15 Because Numeric values will be added Q.51) What will be the output of the program? class Test public static void main(string [] args) int x= 0; int y= 0; for (int z = 0; z < 5; z++) if (( ++x > 2 ) && (++y > 2)) x++; System.out.println(x + " " + y); A. 5 2 B. 5 3 C. 6 3 D. 6 4 ANSWER : 6 3 In the first two iterations x is incremented once and y is not because of the short circuit && operator. In the third and forth iterations x and y are each incremented, and in the fifth iteration x is doubly incremented and y is incremented. Q.52) What is the output of relational operators? Page 18

A. Integer B. Boolean C. Characters D. Double ANSWER : Boolean Q.53) What is the output of this program? class Relational_operator int var1 = 5; int var2 = 6; System.out.print(var1 > var2); A. 1 B. 0 C. true D. false ANSWER : false Operator > returns a boolean value. 5 is not greater than 6 therefore false is returned. output: $ javac Relational_operator.java $ java Relational_operator false Q.54) Which of these operators can skip evaluating right hand operand? A.! B. C. & D. && ANSWER : && Operator short circuit and, &&, and short circuit or,, skip evaluating right hand operand when output can be determined by left operand alone. Q.55) What is the output of this program? class Output int a = 1; int b = 2; int c = 3; a = 4; b >>= 1; c <<= 1; a ^= c; System.out.println(a + " " + b " " + c); Page 19

A. 3 1 6 B. 2 2 3 C. 2 3 4 D. 3 3 6 ANSWER : 3 1 6 output: $ javac Output.java $ java Output 3 1 6 Q.56) Which of the following are legal lines of Java code for increasing value of variable x by 1? 1. x++; 2. x = x + 1; 3. x += 1; 4. x =+ 1; A. 1, 2 & 3 B. 1 & 4 C. 1, 2 & 4 D. 3 & 2 ANSWER : 1, 2 & 3 Operator ++ increases value of variable by 1. x = x + 1 can also be written in shorthand form as x += 1. Q.57) What will be the output of the program? class SC2 public static void main(string [] args) SC2 s = new SC2(); s.start(); void start() int a = 3; int b = 4; System.out.print(" " + 7 + 2 + " "); System.out.print(a + b); System.out.print(" " + a + b + " "); System.out.print(foo() + a + b + " "); System.out.println(a + b + foo()); String foo() return "foo"; A. 9 7 7 foo 7 7foo B. 72 34 34 foo34 34foo C. 9 7 7 foo34 34foo D. 72 7 34 foo34 7foo ANSWER : 72 7 34 foo34 7foo Page 20

Because all of these expressions use the + operator, there is no precedence to worry about and all of the expressions will be evaluated from left to right. If either operand being evaluated is a String, the + operator will concatenate the two operands; if both operands are numeric, the + operator will add the two operands. Q.58) What is the output of this program? class Output int x, y = 1; x = 10; if (x!= 10 && x / 0 == 0) System.out.println(y); else System.out.println(++y); A. 1 B. 2 C. Runtime error owing to division by zero in if condition. D. Unpredictable behavior of program. ANSWER : 2 Operator short circuit and, &&, skips evaluating right hand operand if left hand operand is false thus division by zero in if condition does not give an error. output: $ javac Output.java $ java Output 2 Q.59) What will be the output of the program? class Bitwise public static void main(string [] args) int x = 11 & 9; int y = x ^ 3; System.out.println( y 12 ); A. 0 B. 7 C. 8 D. 14 ANSWER : 14 The & operator produces a 1 bit when both bits are 1. The result of the & operation is 9. The ^ operator produces a 1 bit when exactly one bit is 1; the result of this operation is 10. The operator produces a 1 bit when at least one bit is 1; the result of this operation is 14. Q.60) What is the output of this program? class increment Page 21

A. 25 B. 24 C. 32 D. 33 int g = 3; System.out.print(++g * 8); ANSWER : 32 Operator ++ has more preference than *, thus g becomes 4 and when multiplied by 8 gives 32. output: $ javac increment.java $ java increment 32 Q.61) What will be the output of the program? class Two byte x; class PassO public static void main(string [] args) PassO p = new PassO(); p.start(); void start() Two t = new Two(); System.out.print(t.x + " "); Two t2 = fix(t); System.out.println(t.x + " " + t2.x); Two fix(two tt) tt.x = 42; return tt; A. null null 42 B. 0 0 42 C. 0 42 42 D. 0 0 0 ANSWER : 0 42 42 In the fix() method, the reference variable tt refers to the same object (class Two) as the t reference variable. Updating tt.x in the fix() method updates t.x (they are one in the same object). Remember also that the instance variable x in the Two class is initialized to 0. Q.62) What is the output of this program? Page 22

class bitwise_operator int var1 = 42; int var2 = ~var1; System.out.print(var1 + " " + var2); A. 42 42 B. 43 43 C. 42-43 D. 42 43 ANSWER : 42-43 Unary not operator, ~, inverts all of the bits of its operand. 42 in binary is 00101010 in using ~ operator on var1 and assigning it to var2 we get inverted value of 42 i:e 11010101 which is -43 in decimal. output: $ javac bitwise_operator.java $ java bitwise_operator 42-43 Q.63) Which two statements are equivalent? 16*4 16>>2 16/2^2 16>>>2 A. 1 and 2 B. 2 and 4 C. 3 and 4 D. 1 and 3 ANSWER : 2 and 4 (2) is correct. 16 >> 2 = 4 (4) is correct. 16 >>> 2 = 4 (1) is wrong. 16 * 4 = 64 (3) is wrong. 16/2 ^ 2 = 10 Q.64) What is the output of this program? class increment double var1 = 1 + 5; double var2 = var1 / 4; int var3 = 1 + 5; int var4 = var3 / 4; System.out.print(var2 + " " + var4); A. 1 1 B. 0 1 C. 1.5 1 D. 1.5 1.0 Page 23

ANSWER : 1.5 1 output: $ javac increment.java $ java increment 1.5 1 Q.65) What is the output of this program? class Output int x, y; x = 10; x++; --x; y = x++; System.out.println(x + " " + y); A. 11 11 B. 10 10 C. 11 10 D. 10 11 ANSWER : 11 10 x is initialized to 10 then increased by 1 by ++ operator making it 11. x is again decreased by operator making it 10, next x is incremented by post increment and intialized to y, here the value of x obtained before increment operator is executed, so value of y is 10 and value of x is 11. output: $ javac Output.java $ java Output 11 10 Q.66) What is the output of this program? class bool_operator boolean a = true; boolean b =!true; boolean c = a b; boolean d = a & b; boolean e = d? b : c; System.out.println(d + " " + e); A. false false B. true ture C. true false D. false true ANSWER : false true Operator returns true if any one operand is true, thus 'c = true false' is true. Operator & returns a true if both of the operand is true thus d is false. Ternary operator?: assigns left of ':' if condition is true and right hand of ':' if condition is false. d is false thus e = d? Page 24

b : c, assigns c to e, e contains true. output: $ javac bool_operator.java $ java bool_operator false true Q.67) Which of these statements are incorrect A. Equal to operator has least precedence. B. Brackets () have highest precedence. C. Division operator, /, has higher precedence than multiplication operator. D. Addition operator, +, and subtraction operator have equal precedence. ANSWER : Division operator, /, has higher precedence than multiplication operator. Division operator, /, has equal precedence as of multiplication operator. In expression involving multiplication and division evaluation of expression will begin from right side when no brackets are used. Q.68) Which of these statements are incorrect? A. The left shift operator, <<, shifts all of the bite in a value to the left specified number of times. B. The right shift operator, >>, shifts all of the bite in a value to the right specified number of times. C. The left shift operator can be used as an alternative to multiplying by 2. D. The right shift operator automatically fills the higher order bits with 0. ANSWER : The right shift operator automatically fills the higher order bits with 0. The right shift operator automatically fills the higher order bit with its previous contents each time a shift occurs. This also preserves the sign of the value. Q.69) What will be the output of the program? class SSBool public static void main(string [] args) boolean b1 = true; boolean b2 = false; boolean b3 = true; if ( b1 & b2 b2 & b3 b2 ) /* Line 8 */ System.out.print("ok "); if ( b1 & b2 b2 & b3 b2 b1 ) /*Line 10*/ System.out.println("dokey"); A. ok B. dokey C. ok dokey D. No output is produced ANSWER : dokey The & operator has a higher precedence than the operator so that on line 8 b1 and b2 are evaluated together as are b2 & b3. The final b1 in line 10 is what causes that if test to be true. Hence it prints "dokey". Page 25

Q.70) What is the output of this program? class Output int x, y = 1; x = 10; if (x!= 10 && x / 0 == 0) System.out.println(y); else System.out.println(++y); A. 1 B. 2 C. Runtime error owing to division by zero in if condition. D. Unpredictable behavior of program. ANSWER : 2 Operator short circuit and, &&, skips evaluating right hand operand if left hand operand is false thus division by zero in if condition does not give an error. output: $ javac Output.java $ java Output 2 Q.71) Which of these have highest precedence? A. () B. ++ C. * D. >> ANSWER : () Order of precedence is (highest to lowest) a -> b -> c -> d. Q.72) What is the output of this program? class leftshift_operator byte x = 64; int i; byte y; i = x << 2; y = (byte) (x << 2) System.out.print(i + " " + y); A. 0 64 B. 64 0 C. 0 256 D. 256 0 ANSWER : 256 0 output: $ javac leftshift_operator.java Page 26

$ java leftshift_operator 256 0 Q.73) What will be the output of the program? class BitShift public static void main(string [] args) int x = 0x80000000; System.out.print(x + " and "); x = x >>> 31; System.out.println(x); A. -2147483648 and 1 B. 0x80000000 and 0x00000001 C. -2147483648 and -1 D. 1 and -2147483648 ANSWER : -2147483648 and 1 A is correct. The >>> operator moves all bits to the right, zero filling the left bits. The bit transformation looks like this: Before: 1000 0000 0000 0000 0000 0000 0000 0000 After: 0000 0000 0000 0000 0000 0000 0000 0001 C is incorrect because the >>> operator zero fills the left bits, which in this case changes the sign of x, as shown. B is incorrect because the output method print() always displays integers in base 10. D is incorrect because this is the reverse order of the two output numbers. Q.74) What will be the output of the program? class Test public static void main(string [] args) int x= 0; int y= 0; for (int z = 0; z < 5; z++) if (( ++x > 2 ) (++y > 2)) x++; System.out.println(x + " " + y); A. 5 3 B. 8 2 C. 8 3 D. 8 5 ANSWER : 8 2 The first two iterations of the for loop both x and y are incremented. On the third iteration x is incremented, and for the first time becomes greater than 2. The short circuit or operator keeps y from ever being incremented again and x is incremented twice on each of the last three iterations. Page 27

Q.75) import java.awt.button; class CompareReference public static void main(string [] args) float f = 42.0f; float [] f1 = new float[2]; float [] f2 = new float[2]; float [] f3 = f1; long x = 42; f1[0] = 42.0f; which three statements are true? 1. f1 == f2 2. f1 == f3 3. f2 == f1[1] 4. x == f1[0] 5. f == f1[0] A. 1, 2 and 3 B. 2, 4 and 5 C. 3, 4 and 5 D. 1, 4 and 5 ANSWER : 2, 4 and 5 (2) is correct because the reference variables f1 and f3 refer to the same array object. (4) is correct because it is legal to compare integer and floating-point types (5) is correct because it is legal to compare a variable with an array element. (3) is incorrect because f2 is an array object and f1[1] is an array element. Q.76) Modulus operator, %, can be applied to which of these? A. Integers B. Floating point numbers C. Both Integers and floating point numbers. D. None of the mentioned ANSWER : Integers Modulus operator can be applied to both integers and floating point numbers. Q.77) Which two are equal? 32/4 (8 >> 2) << 4 2^5 128 >>> 2 2 >> 5 A. 1 and 2 B. 2 and 4 C. 1 and 3 D. 2 and 3 ANSWER : 2 and 4 (2) and (4) are correct. (2) and (4) both evaluate to 32. (2) is shifting bits right then left using the signed bit shifters >> and <<. (4) is shifting bits using the unsigned operator >>>, but since the beginning number is positive the sign is maintained. Page 28

(1) evaluates to 8, (3) looks like 2 to the 5th power, but ^ is the Exclusive OR operator so (3) evaluates to 7. (5) evaluates to 0 (2 >> 5 is not 2 to the 5th). Q.78) What is the output of this program? class ternary_operator int x = 3; int y = ~ x; int z; z = x > y? x : y; System.out.print(z); A. 0 B. 1 C. 3 D. -4 ANSWER : 3 output: $ javac ternary_operator.java $ java ternary_operator 4 Q.79) What is the output of this program? class Modulus double a = 25.64; int b = 25; a = a % 10; b = b % 10; System.out.println(a + " " + b); A. 5.64 5 B. 5.0 5.0 C. 5 5 D. 5 5.64 ANSWER : 5.64 5 Modulus operator returns the remainder of a division operation on the operand. a = a % 10 returns 25.64 % 10 i:e 5.64. Similarly b = b % 10 returns 5. output: $ javac Modulus.java $ java Modulus 5.64 5 Q.80) Which of these lines of code will give better performance? 1. a 4 + c >> b & 7; 2. (a ((( 4 * c ) >> b ) & 7 )) A. 1 will give better performance as it has no parentheses. Page 29

B. 2 will give better performance as it has parentheses. C. Both 1 & 2 will give equal performance. D. Dependent on the computer system. ANSWER : Both 1 & 2 will give equal performance. Parentheses do not degrade the performance of the program. Adding parentheses to reduce ambiguity does not negatively affect your system. Q.81) Which of these is not a bitwise operator? A. & B. &= C. = D. <= ANSWER : <= <= is a relational operator. Q.82) What is the output of this program? class operators int var1 = 5; int var2 = 6; int var3; var3 = ++ var2 * var1 / var2 + var2; System.out.print(var2); A. 10 B. 11 C. 12 D. 56 ANSWER : 12 Operator ++ has the highest precedence than /, * and +. var2 is incremented to 7 and then used in expression, var3 = 7 * 5 / 7 + 7, gives 12. output: $ javac operators.java $ java operators 12 Q.83) What will be the output of the program? class Test public static void main(string [] args) Test p = new Test(); p.start(); void start() boolean b1 = false; boolean b2 = fix(b1); System.out.println(b1 + " " + b2); Page 30

boolean fix(boolean b1) b1 = true; return b1; A. true true B. false true C. true false D. false false ANSWER : false true The boolean b1 in the fix() method is a different boolean than the b1 in the start() method. The b1 in the start() method is not updated by the fix() method. Q.84) Which of the following are legal lines of code? int w = (int)888.8; byte x = (byte)1000l; long y = (byte)100; byte z = (byte)100l; A. 1 and 2 B. 2 and 3 C. 3 and 4 D. All statements are correct. ANSWER : All statements are correct. Statements (1), (2), (3), and (4) are correct. (1) is correct because when a floating-point number (a double in this case) is cast to an int, it simply loses the digits after the decimal. (2) and (4) are correct because a long can be cast into a byte. If the long is over 127, it loses its most significant (leftmost) bits. (3) actually works, even though a cast is not necessary, because a long can store a byte. Q.85) What is the value stored in x in following lines of code? int x, y, z; x = 0; y = 1; x = y = z = 8; A. 0 B. 1 C. 9 D. 8 ANSWER : 8 Q.86) What is the output of this program? class Output int a = 1; int b = 2; Page 31

A. 3 2 4 B. 3 2 3 C. 2 3 4 D. 3 4 4 int c; int d; c = ++b; d = a++; c++; b++; ++a; System.out.println(a + " " + b " " + c); ANSWER : 3 4 4 output: $ javac Output.java $ java Output 3 4 4 Q.87) What is the output of this program? class Output boolean a = true; boolean b = false; boolean c = a ^ b; System.out.println(!b); A. 0 B. 1 C. false D. true ANSWER : false output: $ javac Output.java $ java Output False Q.88) On applying Left shift operator, <<, on an integer bits are lost one they are shifted past which position bit? A. 1 B. 32 C. 33 D. 31 ANSWER : 31 The left shift operator shifts all of the bite in a value to the left specified number of times. For each shift left, the high order bit is shifted out and lost, zero is brought in from right. When a left shift is applied to an integer operand, bits are lost once they are shifted past the bit position 31. Page 32

Q.89) What is the output of this program? class bitwise_operator int a = 3; int b = 6; int c = a b; int d = a & b; System.out.println(c + " " + d); A. 7 2 B. 7 7 C. 7 5 D. 5 2 ANSWER : 5 2 And operator produces 1 bit if both operand are 1. Or operator produces 1 bit if any bit of the two operands in 1. output: $ javac bitwise_operator.java $ java bitwise_operator 7 2 Q.90) What is the operand of arithmetic operators? A. Numeric B. Boolean C. Characters D. Both a & c ANSWER : Both a & c The operand of arithmetic operators can be any of numeric or character type, But not boolean. Q.91) What is the output of this program? class ternary_operator int x = 3; int y = ~ x; int z; z = x > y? x : y; System.out.print(z); A. 0 B. 1 C. 3 D. -4 ANSWER : 3 output: $ javac ternary_operator.java $ java ternary_operator Page 33

4 Q.92) Which of the following operators can operate on a boolean variable? 1. && 2. == 3.?: 4. += A. 3 & 2 B. 1 & 4 C. 1, 2 & 4 D. 1, 2 & 3 ANSWER : 1, 2 & 3 Operator Short circuit AND, &&, equal to, ==, ternary if-then-else,?:, are boolean logical operators. += is an arithmetic operator it can operate only on numeric values. Q.93) What will be the output of the program? class Equals public static void main(string [] args) int x = 100; double y = 100.1; boolean b = (x = y); /* Line 7 */ System.out.println(b); A. true B. false C. Compilation fails D. An exception is thrown at runtime ANSWER : Compilation fails The code will not compile because in line 7, the line will work only if we use (x==y) in the line. The == operator compares values to produce a boolean, whereas the = operator assigns a value to variables. A, B, and D are incorrect because the code does not get as far as compiling. If we corrected this code, the output would be false. Q.94) Which operator is used to invert all the digits in binary representation of a number? A. ~ B. <<< C. >>> D. ^ ANSWER : ~ Unary not operator, ~, inverts all of the bits of its operand in binary representation. Q.95) What will be the output of the program? class BoolArray boolean [] b = new boolean[3]; Page 34

int count = 0; void set(boolean [] x, int i) x[i] = true; ++count; public static void main(string [] args) BoolArray ba = new BoolArray(); ba.set(ba.b, 0); ba.set(ba.b, 2); ba.test(); void test() if ( b[0] && b[1] b[2] ) count++; if ( b[1] && b[(++count - 2)] ) count += 7; System.out.println("count = " + count); A. count = 0 B. count = 2 C. count = 3 D. count = 4 ANSWER : count = 3 The reference variables b and x both refer to the same boolean array. count is incremented for each call to the set() method, and once again when the first if test is true. Because of the && short circuit operator, count is not incremented during the second if test. Q.96) Which right shift operator preserves the sign of the value? A. << B. >> C. <<= D. >>= ANSWER : >> Q.97) What is the output of this program? class rightshift_operator int x; x = 10; x = x >> 1; System.out.println(x); A. 10 Page 35

B. 5 C. 2 D. 20 ANSWER : 5 Right shift operator, >>, devides the value by 2. output: $ javac rightshift_operator.java $ java rightshift_operator 5 Q.98) What should be expression1 evaluate to in using ternary operator as in this line? expression1? expression2 : expression3 A. Integer B. Floating ~Ac^a'not^aEURoe point numbers C. Boolean D. None of the mentioned ANSWER : Boolean The controlling condition of ternary operator must evaluate to boolean. Q.99) public class Test What is the prototype of the default constructor? A. Test( ) B. Test(void) C. public Test( ) D. public Test(void) ANSWER : public Test( ) A and B are wrong because they use the default access modifier and the access modifier for the class is public (remember, the default constructor has the same access modifier as the class). D is wrong. The void makes the compiler think that this is a method specification - in fact if it were a method specification the compiler would spit it out. Q.100) public class Outer public void someoutermethod() //Line 5 public class Inner public static void main(string[] argv) Outer ot = new Outer(); //Line 10 Which of the following code fragments inserted, will allow to compile? A. new Inner(); //At line 5 B. new Inner(); //At line 10 Page 36

C. new ot.inner(); //At line 10 D. new Outer.Inner(); //At line 10 ANSWER : new Inner(); //At line 5 A compiles without problem. B gives error - non-static variable cannot be referenced from a static context. C package ot does not exist. D gives error - non-static variable cannot be referenced from a static context. Q.101) Which of these keywords cannot be used for a class which has been declared final? A. abstract B. extends C. Both a & b D. None of the mentioned ANSWER : abstract A abstract class is incomplete by itself and relies upon its subclasses to provide complete implementation. If we declare a class final then no class can inherit that class, an abstract class needs its subclasses hence both final and abstract cannot be used for a same class. Q.102) Which of these is used as default for a member of a class if no access specifier is used for it? A. private B. public C. public, within its own package D. protected ANSWER : private When we pass an argument by call-by-value a copy of argument is made into the formal parameter of the subroutine and changes made on parameters of subroutine have no effect on original argument, they remain the same. Q.103) What will be the output of the program? public class ArrayTest public static void main(string[ ] args) float f1[ ], f2[ ]; f1 = new float[10]; f2 = f1; System.out.println("f2[0] = " + f2[0]); A. It prints f2[0] = 0.0 B. It prints f2[0] = NaN C. An error at f2 = f1; causes compile to fail. D. It prints the garbage value. ANSWER : It prints f2[0] = 0.0 A is correct. When you create an array (f1 = new float[10];) the elements are initialises to the default values for the primitive data type (float in this case - 0.0), so f1 will contain Page 37

10 elements each with a value of 0.0. f2 has been declared but has not been initialised, it has the ability to reference or point to an array but as yet does not point to any array. f2 = f1; copies the reference (pointer/memory address) of f1 into f2 so now f2 points at the array pointed to by f1. This means that the values returned by f2 are the values returned by f1. Changes to f1 are also changes to f2 because both f1 and f2 point to the same array. Q.104) What will be the output of the program? class Super public Integer getlength() return new Integer(4); public class Sub extends Super public Long getlength() return new Long(5); public static void main(string[] args) Super sooper = new Super(); Sub sub = new Sub(); System.out.println( sooper.getlength().tostring() + "," + sub.getlength().tostring() ); A. 4, 4 B. 4, 5 C. 5, 4 D. Compilation fails. ANSWER : Compilation fails. D is correct, compilation fails - The return type of getlength( ) in the super class is an object of reference type Integer and the return type in the sub class is an object of reference type Long. In other words, it is not an override because of the change in the return type and it is also not an overload because the argument list has not changed Q.105) Which of the following statements are incorrect? A. public members of class can be accessed by any code in the program. B. private members of class can only be accessed by other members of the class. C. private members of class can be inherited by a sub class, and become protected members in sub class. D. protected members of a class can be inherited by a sub class, and become private members of the sub class. ANSWER : private members of class can be inherited by a sub class, and become protected members in sub class. private members of a class cannot be inherited by a sub class. Q.106) What is the stored in the object obj in following lines of code? box obj; Page 38

A. Memory address of allocated memory of object. B. NULL C. Any arbitrary pointer D. Garbage ANSWER : NULL Memory is allocated to an object using new operator. box obj; just declares a reference to object, no memory is allocated to it hence it points to NULL. Q.107) You want subclasses in any package to have access to members of a superclass. Which is the most restrictive access that accomplishes this objective? A. public B. private C. protected D. transient ANSWER : protected Access modifiers dictate which classes, not which instances, may access features. 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. Q.108) What 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? A. public B. abstract C. protected D. default access ANSWER : default access default access is the "package oriented" access modifier. A and C are wrong because public and protected are less restrictive. B and D are wrong because abstract and synchronized are not access modifiers. Q.109) Given a method in a protected class, what access modifier do you use to restrict access to that method to only the other members of the same class? Page 39

A. final B. static C. private D. protected ANSWER : protected The private access modifier limits access to members of the same class. A, B, D, and E are wrong because protected are the wrong access modifiers, and final, static, and volatile are modifiers but not access modifiers. Q.110) Which of the following is a valid declaration of an object of class Box? A. Box obj = new Box(); B. Box obj = new Box; C. obj = new Box(); D. new Box obj; ANSWER : Box obj = new Box(); new is keyword used to allocate dynamic memory. Box obj = new Box(); This is syntax to create object in java. Q.111) Which of the following class level (nonlocal) variable declarations will not compile? A. protected int a; B. transient int b = 3; C. private synchronized int e; D. volatile int d; ANSWER : private synchronized int e; C will not compile; the synchronized modifier applies only to methods. A and B will compile because protected and transient are legal variable modifiers. D will compile because volatile is a proper variable modifier. Q.112) What is the output of this program? class main_class int x = 9; if (x == 9) int x = 8; System.out.println(x); A. 9 B. 8 C. Compilation error D. Runtime error ANSWER : Compilation error Two variables with the same name can't be created in a class. output: Page 40

$ javac main_class.java Exception in thread main java.lang.error: Unresolved compilation problem: Duplicate local variable x Q.113) /* Missing statements? */ public class NewTreeSet extends java.util.treeset public static void main(string [] args) java.util.treeset t = new java.util.treeset(); t.clear(); public void clear() TreeMap m = new TreeMap(); m.clear(); which two statements, added independently at beginning of the program, allow the code to compile? No statement is required import java.util.*; import.java.util.tree*; import java.util.treeset; import java.util.treemap; A. 1 only B. 2 and 5 C. 3 and 4 D. 3 and 5 ANSWER : 2 and 5 (2) and (5). TreeMap is the only class that must be imported. TreeSet does not need an import statement because it is described with a fully qualified name. (1) is incorrect because TreeMap must be imported. (3) is incorrect syntax for an import statement. (4) is incorrect because it will not import TreeMap, which is required. Q.114) Which of the following statements is correct? A. Public method is accessible to all other classes in the hierarchy B. Public method is accessible only to subclasses of its parent class C. Public method can only be called by object of its class. D. Public method can be accessed by calling object of the public class. ANSWER : Public method is accessible to all other classes in the hierarchy we can access public member of class anywhere in program. Q.115) Which of these operators is used to allocate memory for an object? A. malloc B. alloc C. new D. give ANSWER : new Operator new dynamically allocates memory for an object and returns a reference to it. Page 41