Sie sind auf Seite 1von 32

1) 1*compiletime error at lines 1" "3"!

Class C { *compiletime error at line 3 public static void main(String[] args) { 3*compiletime error at line 1 int[]a1[]=new int[3][3]; //3 !*compiletime error at lines 3"! int a [!]={3"!"#"$%; //! #*.one o( t'e above int a [#]; //# /nswer0 ! %% ,-planation0 &'at is t'e result o( attempting to compile and run t'e program )* all met'ods declared wit'in an inter(ace are implicitl2 public" a wea1er access level can not 1*compiletime error at lines 3"!"# be declared* *compiltime error at line !"# 3*compiletime error at line 3 !*+untime ,-ception #*.one o( t'e above /ns0 ,-planation0 no value s'oud be speci(ied in t'e rig'tsidebrac1ets w'en constructing an arra2 3) class C{ int i; public static void main (String[] args) { int i; //1 private int a = 1; // protected int b = 1; //3 public int c = 1; //! S2stem*out*println(a4b4c); //# ) %% inter(ace 3{ 1*compiletime error at lines 1" "3"!"# void (1(); // 1 compiletime error at lines "3"!"# public void ( (); // 3*compiletime error at lines "3"! protected void (3(); // 3 !*prints 3 private void (!(); // ! #*.one o( t'e above % /nswer w'ic' lines generate compile time errors)

,-planation0

S2stem*out*println(s);

5'e access modi(iers public" protected and % private" can not be applied to variables declared inside met'ods* void m1(); !) class C { public static void main (String[] a1) { S2stem*out*print(a1[1] 4 a1[ ] 4 a1[3]); %% { int -=1; m (-); S2stem*out*println(-4::); %

&'at is t'e result o( attempting to compile and void m (int -){ run t'e program) -=-; ; 6ava command / 7 C 1*8rints0 /7C *8rints 7C and +untime ,-ception 3*8rints0 7C9 !*+untime ,-ception #*.one o( t'e above /nswer ,-planation0 arra2 inde- outo( bounds e-ception onl2 till a1[ ] is allowed* #) class C{ $) static int s; class C { public static void main(String a[]){ public static void main(String[] args) { C ob6=new C(); int i1=1; ob6*m1(); switc'(i1){ s=-; %% 1*prints 1" *prints "< 3*prints " !*compile time error #*.oneo(t'e above /nswer0 1 ,-planation0 =nl2 ob6ects and arra2s are passed b2 re(erence*ot'er are passed b2 value*s is a static variable w'ic' is global to t'e class

case 10 S2stem*out*println(:one:); case 0 S2stem*out*println(:two:); case 30 S2stem*out*println(:t'ree:); %%%

1*@in1ed@ist *5reeAap 3*Bas'Aap !*Bas'Set #*.one o( t'e above /nswer0 ! C)

&'at is t'e result o( attempting to compile and class C1 run t'e program) { 1*prints one two t'ree *prints one 3*compile time error !*+untime e-ception( #*.one o( t'e above /nswer0 1 ,-planation0 5'ere is no brea1 statement in case 1 so it causes t'e below case statements to e-ecute regardless o( t'eir values >) ,ac' element must be uni?ue 9uplicate elements must not replace old elements* ,lements are not 1e2/value pairs* &'at is t'e result o( attempting to compile and /ccessing an element can be almost as (ast as run t'e program) per(orming a similar operation on an arra2* 1*prints ob6ect created &'ic' o( t'ese classes provide t'e speci(ied (eatures) *Compile time error static inter(ace 3 { static class C { % % public static void main(String a[]) { C1*3*C ob1=new C1*3*C (); S2stem*out*println(:ob6ect created:); % %

3*+untime ,-cepion !*.one o( t'e above /nswer0 1

,-planation0 static members can be accessed wit'out instantiating t'e particular class 1<)

,-planation0 / static inter(ace or class can contain static members*Static members can be accessed wit'out instantiating t'e particular class D) class C1 { static class C { static int i1; 11) % public static void main(String a[]) { S2stem*out*println(C1*C *i1); % % class C{ static int (1(int i) { S2stem*out*print(i 4 :":); return <; % public static void main (String[] args) { int i = <; i = i44 4 (1(i); S2stem*out*print(i); %% 5'e range o( negative numbers is greater b2 1 t'an t'e range o( positive numbers* / signed data t2pe 'as an e?ual number o( nonEFero positive and negative values available 1*true *(alse /nswer0 ,-planation0

8rints0 <"< &'at is t'e result o( attempting to compile and run t'e program) 8rints0 1"< 1*prints < *Compile time error 3*+untime e-ception !*.one o( t'e above /nswer0 1 class C{ static String m(int i) {return :int:;% static String m((loat i) {return :(loat:;% public static void main (String[] args) { 8rints0 <"1 CompileEtime error* 1 )

long a1 = 1; double b1 = ; S2stem*out*print(m(a1)4:":4 m(b1)); %% 8rints0 (loat"double 8rints0 (loat"(loat 8rints0 double"(loat CompileEtime error .one o( t'e above ,-planation0 .o ,-planation /vailable /ns0 !

,-planation0 .o ,-planation /vailable /ns0 1!) 1* String7u((er s1 = new String7u((er(:abc:); * String7u((er s = s1; 3* String7u((er s3 = new String7u((er(:abc:); Bow man2 ob6ects are created ) < /ns0 1 /ns0 /ns0 3

13) class C { public static void main(String a[]) { C c1=new C(); C c =m1(c1); C c3=new C(); c =c3; //$ anot'ermet'od(); % static C m1(C ob1){ ob1 =new C(); return ob1; % % /(ter line $" 'ow man2 ob6ects are eligible (or garbage collection) /ns0 1 /ns0 /ns0 3 /ns0 ! .one o( t'e above ,-planation0 .o ,-planation /vailable /ns0 ! 1#) class c { { S2stem*out*println(:initialiFer:); % public static void main(String a[]) { S2stem*out*println(:main:); c ob1=new c (); % % prints main and initialiFer prints initialiFer and main compile time error .one o( t'e above ,-planation0 .o ,-planation /vailable

/ns0 1 1$) class c1 { public static void main(String a[]) { c1 ob1=new c1(); =b6ect ob =ob1; S2stem*out*println(ob instanceo( =b6ect); S2stem*out*println(ob instanceo( c1); % % 8rints true"(alse 8rint (alse"true 8rints true"true compile time error .one o( t'e above ,-planation0 .o ,-planation /vailable /ns0 3

.one o( t'e above ,-planation0 a*run() met'od was called instead o( a*start(); so t'e (ull program runs as a single t'read so a*run() is guaranteed to complete /ns0 1C) class bi1e { % class arr e-tends bi1e{ public static void main(String[] args) { arr[] a1=new arr[ ]; bi1e[] a ; a =a1; //3 arr[] a3; a3=a1; //# %% compile time error at line 3 compile time error at line # +untime e-ception 5'e code runs (ine

1>) .one o( t'e above class / e-tends 5'read { private int i; public void run() {i = 1;% public static void main(String[] args) { / a = new /(); a*run(); S2stem*out*print(a*i); %% 8rints not'ing 8rints0 1 8rints0 <1 CompileEtime error ,-planation0 bi1e is t'e superclass o( arr*so t'e2 are compatible(superob6ect=subob6ect) but subob6ect=superob6ect not allowed /ns0 ! 1D) class C{ public static void main (String[] args) { String s1=:'6'':; // 1 String s =:Gu<<< :; // String s3=:HGGH:; //3 %%

compile time error at line 1 compile time error at line compile time error at line 3 +untime e-ception t'e code runs wit'out an2 error ,-planation0 / String literal is a se?uence o( c'aracters enclosed in double ?uotes /ns0 # <) &'ic' data t2pe is wider (or t'e purpose o( casting0 (loat or long)

none o( t'e above ,-planation0 b141 returns an integer value w'ic' can not be assigned to a b2te variable /ns0

SCI8 Sample ?uestions 5'e (ollowing ?uestions are sample ?uestions (or SCI8 3( an2 ?ueries regarding t'e ?uestions or answers please mail us

1) (loat long ,-planation0 (loat is wider t'an long" because t'e entire range o( long (its wit'in t'e range o( (loat* /ns0 1 1) class C{ public static void main (String[] args) { b2te b1=33; //1 b144; // b2te b =##; //3 b =b141; //! S2stem*out*println(b14::4b ); %% compile time error at line compile time error at line ! prints 3!"#$ runtime e-ception class C{ public static void main (String[] args) { b2te b1=33; //1 b144; // b2te b =##; //3 b =b141; //! S2stem*out*println(b14::4b ); %% compile time error at line compile time error at line ! prints 3!"#$ runtime e-ception none o( t'e above ,-planation0 b141 returns an integer value w'ic' can not be assigned to a b2te variable /ns0

+untime e-ception ) .one o( t'e above import 6ava*util*;; class C { (inal Jector v; C() { v=new Jector(); % C(int i) { % public void someAet'od() { S2stem*out*println(v*is,mpt2()); % % compile time error runtime e-ception t'e code compiles and runs (ine none o( t'e above ,-planation0 .o ,-planation /vailable /ns0 1 3) class C1{ public void m1(){ // 1 % % class C e-tends C1{ // private void m1(){ % % compile time error at line1 compile time error at line /ns0 ,-planation0 tending to assign wea1er access not allowed /ns0 !) inter(ace 3{ int i; // line 1 % class C implements 3{ public static void main(String a[]){ S2stem*out*println(i); S2stem*out*println(i44); //line % % compile time error at line 1" compile time error at line +untime e-ception .oneo(t'e above ,-planation0 inter(ace constants are (inal so"t'e2 must be initialiFed w'en declaring it and t'e2 can not be altered /ns0 1 #) /n abstract class must 'ave at least one abstract met'od true true ,-planation0 /n abstract wit'out abstract met'ods is allowed

$) KL[C9/5/[ class C { public static void main(String[] args) { boolean b1; b1=3K!K#; //1 S2stem*out*println(b1); // %% ]]M compile time error at line 1 compile time error at line /ns0 1 +untime e-ception C) .one o( t'e above ,-planation0 KL[C9/5/[ 3K!K# evaulates to trueK# EEMitHs a wrong e-pression so it results in compiletime error ]]M /ns0 1 >) class C{ public static void main(String[] args) { tr2 { int i1=3/<; % catc'(,-ception e) { S2stem*out*println(:e-ception1:); % catc'(.ull8ointer,-ception e) { S2stem*out*println(:e-ception :); % (inall2 { S2stem*out*println(:(inall2:); % %% class c1{ class C { public static void main(String[] args) { c'ar c1=$#; switc'(c1){ case H/H0 S2stem*out*println(:one:); de(ault0 S2stem*out*println(:two:); case HbH0 S2stem*out*println(:t'ree:); %%% prints one twot 'ree prints two t'ree compile time error +untime e-ception .one o( t'e above ,-planation0 c'ar is a legal value (or switc' clause /ns0 1 D) compile time error runtime e-ception prints e-ception1 and (inall2 prints e-ception1"e-ception and (inall2 .one o( t'e above ,-planation0 .o ,-planation /vailable

void go(){% % class c e-tends c1 { String go() { return null; % % compile time error runtime e-ceptione t'e code compiles and runs (ine .one o( t'e above ,-planation0 .o ,-planation /vailable /ns0 1 3<) class base { base(int c) { S2stem*out*println(:base:); % % class Super e-tends base { Super() { S2stem*out*println(:super:); % public static void main(String [] a) { base b1=new Super(); % % compile time error

.one o( t'e above ,-planation0 .o ,-planation /vailable /ns0 1 31) class c { (inal int i1; c () { i1=i141; % { i1= ; % public static void main(String a[]) { c ob1=new c (); S2stem*out*println(ob1*i1); % % compile time error prints 3 3 ) class C{ public static void main(String a[]) int i1=D; int i ; i((i1M3) { i =C; % S2stem*out*println(i ); %% compile time error +untim error prints C {

runtime e-ceptione prints < t'e code compiles and runs (ine

.one o( t'e above

% %

,-planation0 since variable i1 is not (inal t'e value is not 1nown at compiletime itsel(*so generate compile prints static main time error prints main /ns0 1 prints main static 33) compiletime error class / e-tends 5'read { public void run() { none o( t'e above S2stem*out*print(:/:); % ,-planation0 % .o ,-planation /vailable class 7 { public static void main (String[] args) { /ns0 1 / a = new /(); a*start(); 3#) a*start(); // 1 % class /{ % static String m((loat i) {return :(loat:;% static String m(double i) {return :double:;% public static void main (String[] args) { compile time error int a1 = 1; long b1 = ; S2stem*out*print(m(a1)4:":4 m(b1)); +untime ,-ception %% t'e code compile and runs (ine prints (loat"(oat none o( t'e above prints (loat"double ,-planation0 3( t'e start met'od is invo1ed on a t'read t'at is prints double"double alread2 running" t'en an 3llegal5'readState,-ception will probabl2 be compile time error t'rown .one o( t'e above /ns0 ,-planation0 3!) .o ,-planation /vailable class c1 { static{ S2stem*out*println(:static:); % public static void main(String a[]) { S2stem*out*println(:main:); /ns0 1 3$) &'en a b2te is added to a c'ar" w'at is t'e t2pe o( t'e result)

b2te int long non o( t'e above

&'at is t'e signature o( t'e run() met'od o( t'e +unnable inter(ace) void run() public void run(+unnable target)

,-planation0 public void run() 5'e result o( all arit'metic per(ormed wit' t'e binar2 operators (not t'e assignment operators) public static void run() is an int" a long" a (loat" or a double* Bere b2te and c'ar are promoted to int" so t'e result is an ,-planation0 int* .o ,-planation /vailable /ns0 3>) class C e-tends 5'read{ /ns0 3 3D) class C{ public static void main(String args[]) { int a = 1; a 4= 44a 4 a44; S2stem*out*print(a); %%

public static void main(String argv[]){ C b = new C(); b*run(); % public void start(){ (or (int i = <; i K1<; i44){ compile time error S2stem*out*println(:Jalue o( i = : 4 i); % +untime ,-ception % 8rints # % 8rints ! .one o( t'e above compile time error runtime ,-ception prints values (rom < to D .one o( t'e above ,-planation0 .o ,-planation /vailable /ns0 ! 3C)

,-planation0 .o ,-planation /vailable /ns0 3 !<) inter(ace 3 { //1 public class 3nner { /// 3nner ( ) { S2stem *out * println ( :3nner Created: ) ; %

%; %; compile time error at line 1 compile time error at line t'e code compiles (ine none o( t'e above ,-planation0 .o ,-planation /vailable /ns0 3 !1) abstract class C1{ public void m1(){ %% abstract class C { public void m (){ %% //1 //

line 1" "3"! line 3"! line 3 line "3"!

line 3"!"# ,-planation0 all met'ods declared wit'in an inter(ace are implicitl2 public" a wea1er access level can not be declared /ns0 !3) abstract class ve'icle{ abstract public void speed(); % class car e-tends ve'icle{ public static void main (String args[]) { ve'icle ob1; ob1=new car(); //1 %% compiletime error at line 1

compile time error at line1 compile time error at line 5'e code compiles (ine

(orces t'e class car to be declared as abstract .one o( t'e above +untime ,-ception ,-planation0 since t'e class C is abstract it can contain abstract met'ods /ns0 3 ! ) inter(ace 3{ void (1(); // 1 public void ( (); // protected void (3(); // 3 private void (!(); // ! abstract void (#(); // # % .one o( t'e above ,-planation0 .o ,-planation /vailable /ns0 !!) class command { public static void main (String[] a1) { S2stem*out*println(a1*lengt'()); //1 S2stem*out*println(a1[<]); // S2stem*out*println(a1); //3 //

%% compile time error at line1 compile time error at line compile time error at line3 +untime e-ception ,-planation0 lengt' is not a met'od* itHs a variable /ns0 1 !#)

% public void m1(String o1) { S2stem*out*println(:string:); % public int m1(int c) { return c; % public static void main(String a[]) { c1 ob1=new c1(); ob1*m1(:'ai:); % %

abstract class / {% // 1 transient class 7 {% // print ob6ect private class C {% // 3 static class 9 {% // ! prints string &'ic' o( t'ese declarations will not produce a compileEtime error) compile time error non o( t'e above /ns0 1 /ns0 /ns0 3 /ns0 ! .one o( t'e above ,-planation0 .o ,-planation /vailable /ns0 !>) public static double sin(double angle) &'at are t'e units o( t'e :angle: argument)

,-planation0 5'e modi(iers" private and static" can be applied to a nested class" but can not be applied to a 9egrees class t'at is not nested transient is allowed onl2 (or variables* +adians /ns0 1 !$) class c1 { public void m1(=b6ect o1) { S2stem*out*println(:ob6ect:); 7ot' .one o( t'e above ,-planation0 .o ,-planation /vailable /ns0

!C) class base { base() { S2stem*out*println(:base:); % base(int i1) { % % class Super e-tends base { Super() { S2stem*out*println(:super:); super(1); % public static void main(String [] a) { base b1=new Super(); % % compile time error prints base and super prints super and base

.o ,-planation /vailable /ns0 1 #<) class C { private String get ( String str ) { tr2 { t'row new ,-ception ( ) ; return str ; % catc' ( ,-ception e ) { return null ; % % public static void main ( String peace [ ] ) { tr2 { S2stem*out*println ( ( new C ( ) )*get (:C:)); % catc' ( ,-ception e ) { S2stem*out*println( :,-ception: ) ; % % %; compile time error

none o( t'e above prints ,-ception ,-planation0 .o ,-planation /vailable /ns0 1 !D) 5'e 5'rowable class is t'e superclass o( all e-ceptions in t'e Iava language* true (alse ,-planation0 &'ic' o( t'e (ollowing met'ods are static members o( t'e 5'read class) t'e code compiles and runs (ine none o( t'e above ,-planation0 .o ,-planation /vailable /ns0 1 #1)

6oin run sleep start wait ,-planation0 .o ,-planation /vailable /ns0 3# )

{ S2stem*out*println(9ouble*.a.==9ouble*.a.); S2stem*out*println(9ouble*.a.L =9ouble*.a.); S2stem*out*println(Nloat*.a.==9ouble*.a.); % % prints (alse true (alse print true (alse true prints true true true

class C { public static void main(String[] args) { tr2 { int i1=3/<; % S2stem*out*println(:'ai:); catc'(.ullpointer,-ception e) { % (inall2 { S2stem*out*println(:(inall2); % %% compile time errors print 'ai and (inall2

prints (alse (alse (alse compiletime error ,-planation0 .o ,-planation /vailable /ns0 1 #!) class C { public static void main ( String 1a [ ] ) { 5'read t = 5'read * current5'read ( ) ; t * set8riorit2 ( E 1 ) ; S2stem * out * println ( : 9one L : ) ; % %; compile time error

t'e code compiles and runs (ine +untime ,-ception none o( t'e above 5'e code compiles and runs (ine ,-planation0 .o ,-planation /vailable /ns0 1 #3) /ns0 class c1 { public static void main(String a[]) ##) .one o( t'e above ,-planation0 .o ,-planation /vailable

class c1 { public void m1() { S2stem*out*println(:m1 met'od in C1 class:); % % class c { public c1 m1() { return new c1(){ public void m1() { S2stem*out*println(:m1 me'od in anon2mous class:); %%;% public static void main(String a[]) { c1 ob1 =new c ()*m1(); ob1*m1(); %% prints m1 met'od in C1 class prints m1 met'od in anonumous class compile time error +untime error none o( t'e above

t'row new c1(); % public static void main(String a[]) { % % compile time error +untime ,-ception 5'e code compiles and runs (ine .one o( t'e above ,-planation0 .o ,-planation /vailable /ns0 1 #>) class C { static void m1(=b6ect -) {S2stem*out*print(:=b6ect:);% static void m1(String -) {S2stem*out*print(:String:);% public static void main(String[] args) { m1(null); %% 8rints =b6ect

,-planation0 8rints String t'e anon2mous class overrides t'e m1 met'od in c1*so according to t'e d2namic dispatc' t'e compiletime error m1 met'od in t'e anon2mous is called .one o( t'e above /ns0 ,-planation0 #$) 5'e more speci(ic o( t'e two" m(String -)" is c'osen class c1 e-tends ,-ception {% /ns0 class c { #C) static void m1() {

all classes o( t'e 6ava*lang pac1age are automaticall2 imported true (alse ,-planation0 .o ,-planation /vailable /ns0 1 #D)

.o ,-planation /vailable$1) inter(ace 3{ (inal class C1 { //1 static int i=D;; // % % class C implements 3{ public static void main(String a[]){ S2stem*out*println(3*C1*i); ///3 %% compile time error at line 1

5'e constructor (or t'e Aat' class is private" so compile time error at line it cannot be instaniated compile time error at line 3 true (alse ,-planation0 .o ,-planation /vailable /ns0 1 $<) class C { public static void main(String[] args) { int -= ; int 2=3; i(((2==-44)O(-K442)){ S2stem*out*println(-4::42); % %% prints 3! prints 33 compile time error +untime e-ception .one o( t'e above ,-planation0 % catc'(.ull8ointer,-ception e) { % /ns0 ! ) class C{ public static void main(String[] args) { tr2 { tr2 { tr2 { % catc'(+untime,-ception e) { % % catc'(,-ception e) { % prints D +untime e-ception ,-planation0 inter(aces classes are b2 de(ault static"(inal* so"no compile time errors are genearated

(inall2 { S2stem*out*println(:(inall2:); % %%

prints Dstring prints !#string compile time error

8rints (inall2 compile time error +untime ,-ception .one o( t'e above ,-planation0 .o ,-planation /vailable /ns0 1 $3) class C { public static void main ( String 1a [ ] ) { w'ile ( (alse ) { S2stem*out*println ( :Jalue: ) ; % % % compile time error prints Jalue in(initel2 +untime ,-ception .one o( t'e above ,-planation0 .o ,-planation /vailable /ns0 1

+untime e-ception .one o( t'e above ,-planation0 arguments to t'e met'od are evalutated (rom le(t to rig't so !4#4:string: ==M D4string==MDstring /ns0 1 $#) class B { public static void main (String[] args) { String s1 = :BBB:; String7u((er sb = new String7u((er(s1); S2stem*out*print(sb *e?uals(s1) 4 :": 4 s1*e?uals(sb )); %% 8rints0 (alse"(alse 8rints0 true"(alse 8rints0 (alse"true 8rints0 true"true .one o( t'e above ,-planation0 s1 and sb are pointing to di((erent ob6ect re(erences /ns0 1

$!) $$) class C { public static void main(String[] args) { S2stem*out*println(!4#4:String:); %% 5'e relations'ip between a class and its superclass is

.o ,-planation available 'asEa /ns0 1 is Ea $D) .one o( t'e above ,-planation0 .o ,-planation available /ns0 $>) &'ic' o( t'e (ollowing modi(iers can be applied to a class t'at is not a nested class) public protected

class / e-tends 5'read { private private int i; public void run() {i = 1;% static public static void main(String[] args) { / a = new /(); ,-planation0 a*run(); .o ,-planation /vailable S2stem*out*print(a*i); <) %% Bow man2 t'reads are created in t'is 8rogram) i((<*< == E<*<) { S2stem*out*println(:true:); % /ns0 1 else{ S2stem*out*println(:(alse:); /ns0 % /ns0 3 prints (alse < prints true .one o( t'e above ,-planation0 ,-planation0 .o ,-planation available Aain t'read is a t'read and calling run met'ods will not creat t'read* /ns0 /ns0 1 $C) String ob6ects once created can not be modi(ied SCI8 Sample ?uestions true (alse ,-planation0 3( an2 ?ueries regarding t'e ?uestions or answers please mail us 5'e (ollowing ?uestions are sample ?uestions (or SCI8

5'is statement will 8rint ><) i((<*< == E<*<) { S2stem*out*println(:true:); % else{ S2stem*out*println(:(alse:); % prints (alse /ns0 prints true >3) ,-planation0 .o ,-planation available /ns0 >1) class C { public static void main(String[] args) { double d1 = Aat'*(loor(<*#); double d = Aat'*(loor(1*#); S2stem*out*print(d1 4 :": 4 d ); %% 8rints0 <*<"1*< 8rints0 <*<" *< /ns0 8rints0 1*<"1*< >!) 8rints0 1*<" *< .one o( t'e above ,-planation0 .o ,-planation /vailable Jector /ns0 1 5reeAap > ) 5reeSet S2stem*out*println(:String:*substring(<"!)); &'ic' o( t'e (ollowing classes will not allow uns2nc'roniFed read operations b2 multiple t'reads) will 8rint ,?ual will 8rint .ot ,?ual compile time error none o( t'e above ,-planation0 .o ,-planation /vailable i((:String:*replace(HtH"H5H) == :String:*replace(HtH"H5H)) S2stem*out*println(:,?ual:); else S2stem*out*println(:.ot ,?ual:); will print :Strin: will print :Stri: will cause compiler error none o( t'e above ,-planation0 .o ,-planation /vailable

Bas'Aap Bas'Set ,-planation0 .o ,-planation /vailable /ns0 1 >#) String7u((er ob6ects once created can not be modi(ied true (alse ,-planation0 .o ,-planation available /ns0

R &'ic' o( t'e (ollowing are 1e2words in Iava* Select t'e two correct answers* 1* (riend * .T@@ 3* implement !* s2nc'roniFed #* t'rows R &'ic' o( t'e (ollowing are Iava 1e2words* Select t'e (our correct answers* 1* super * strict(p 3* void !* s2nc'roniFe #* instanceo( R &'ic' o( t'ese are Iava 1e2words* Select t'e (ive correct answers

1* 5+T, * volatile 3* transient !* native PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP #* inter(ace PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP $* t'en PPPPPPPP >* new /; /99353=./@ N/Q ;/ PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP R Tsing up to (our c'aracters" write t'e Iava PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP representation o( octal literal $* PPPPPPPPP R Tsing up to (our c'aracters" write t'e Iava representation o( integer literal 3 in R &'ic' o( t'ese are legal identi(iers* Select t'e 'e-adecimal* t'ree correct answers* R Tsing up to (our c'aracters" write t'e Iava representation o( integer literal 1< in 1* numberP1 'e-adecimal* * numberPa R &'at is t'e minimum value o( c'ar t2pe* 3* S1 3! Select t'e one correct answer* !* Evolatile 1* < R &'ic' o( t'ese are not legal identi(iers* Select * E 1# t'e (our correct answers* 3* E C !* E 1# E 1 1* 1alp'a #* E 1$ * Pabcd $* E 1$ E 1 3* -24abc !* transient R Bow man2 b2tes are used to represent t'e #* accountEnum primitive data t2pe int in Iava* Select t'e one $* ver2PlongPname correct answer*

1* *! R &'at would 'appen w'en t'e (ollowing is 3* C compiled and e-ecuted* Select t'e one correct !* 1 answer* #* 5'e number o( b2tes to represent an int is compiler dependent* class e-ample { R &'at is t'e legal range o( values (or a int -; variable declared as a b2te* Select t'e one int 2; correct answer* String name; public static void main(String args[]) { 1* < to #$ e-ample pnt = new e-ample(); * < to ## S2stem*out*println(:pnt is : 4 pnt*name 4 3* E1 C to 1 > : : 4 pnt*- 4 : : 4 pnt*2); !* E1 C to 1 C % #* E1 > to 1 C % $* E 1# to 1# E 1 1* 5'e program does not compile because -" R 5'e widt' in bits o( double primitive t2pe in 2 and name are not initialiFed* Iava is EE* Select t'e one correct answer* * 5'e program t'rows a runtime e-ception as -" 2" and name are used be(ore initialiFation* 1* 5'e widt' o( double is plat(orm dependent 3* 5'e program prints pnt is < <* * $! !* 5'e program prints pnt is null < <* 3* 1 C #* 5'e program prints pnt is .T@@ (alse (alse !* C #* ! R 5'e initial value o( an instance variable o( t2pe String t'at is not e-plicitl2 initialiFed in t'e R &'at would 'appen w'en t'e (ollowing is program is EE* Select t'e one correct answer* compiled and e-ecuted* Select t'e one correct answer* 1* null * :: 3* .T@@ public class Compare { !* < public static void main(String args[]) { #* 5'e instance variable must be e-plicitl2 int - = 1<" 2; assigned* i((- K 1<) 2 = 1; R 5'e initial value o( a local variable o( t2pe i((-M= 1<) 2 = ; String t'at is not e-plicitl2 initialiFed and w'ic' S2stem*out*println(:2 is : 4 2); is de(ined in a member (unction o( a class* % Select t'e one correct answer* % 1* null 1* 5'e program compiles and prints 2 is < * :: w'en e-ecuted* 3* .T@@ * 5'e program compiles and prints 2 is 1 !* < w'en e-ecuted* #* 5'e local variable must be e-plicitl2 3* 5'e program compiles and prints 2 is assigned* w'en e-ecuted* !* 5'e program does not compile complaining R &'ic' o( t'e (ollowing are legal Iava about 2 not being initialiFed* programs* Select t'e (our correct answers* #* 5'e program t'rows a runtime e-ception*

1* // 5'e comments come be(ore t'e pac1age pac1age p1g; import 6ava*awt*;; class C{% * pac1age p1g; import 6ava*awt*;; class C{% 3* pac1age p1g1; pac1age p1g ; import 6ava*awt*;; class C{% !* pac1age p1g; import 6ava*awt*;; #* import 6ava*awt*;; class C{% $* import 6ava*awt*;; pac1age p1g; class C {% R &'ic' o( t'e (ollowing statements are correct* Select t'e (our correct answers*

* 5'e program compiles and runs generating an output o( :test: 3* 5'e program compiles and runs but does not generate an2 output* !* 5'e program compiles but does not run* R &'ic' o( t'ese are valid declarations (or t'e main met'od) Select t'e one correct answer* 1* public void main(); * public static void main(String args[]); 3* static public void main(String); !* public static void main(String ); #* public static int main(String args[]); R &'ic' o( t'e (ollowing are valid declarations (or t'e main met'od* Select t'e t'ree correct answers*

1* public static void main(String args[]); * public static void main(String []args); 3* (inal static public void main (String args[]); 1* / Iava program must 'ave a pac1age !* public static int main(String args[]); statement* #* public static abstract void main(String * / pac1age statement i( present must be t'e args[]); (irst statement o( t'e program (barring an2 comments)* R &'at 'appens w'en t'e (ollowing program is 3* 3( a Iava program de(ines bot' a pac1age compiled and e-ecuted wit' t'e command E and import statement" t'en t'e import 6ava test* Select t'e one correct answer* statement must come be(ore t'e pac1age statement* !* /n empt2 (ile is a valid source (ile* class test { #* / Iava (ile wit'out an2 class or inter(ace public static void main(String args[]) { de(initions can also be compiled* i((args*lengt' M <) $* 3( an import statement is present" it must S2stem*out*println(args*lengt'); appear be(ore an2 class or inter(ace de(initions* % % R &'at would be t'e results o( compiling and running t'e (ollowing class* Select t'e one 1* 5'e program compiles and runs but does correct answer* not print an2t'ing* * 5'e program compiles and runs and prints < class test { 3* 5'e program compiles and runs and prints public static void main() { 1 S2stem*out*println(:test:); !* 5'e program compiles and runs and prints % % #* 5'e program does not compile* 1* 5'e program does not compile as t'ere is R &'at is t'e result o( compiling and running no main met'od de(ined* t'is program) Select t'e one correct answer*

% public class test { public static void main(String args[]) { int i" 6; int 1 = <; 6= ; 1 = 6 = i = 1; S2stem*out*println(1); % % 1* 5'e program will t'row an /rra23nde-=ut=(7ounds e-ception* * 5'e program will print :6ava test: 3* 5'e program will print :6ava 'appens:; !* 5'e program will print :test 'appens: #* 5'e program will print :lets 'appens: R &'at all gets printed on t'e standard output w'en t'e class below is compiled and e-ecuted b2 entering :6ava test lets see w'at 'appens:* Select t'e two correct answers*

1* 5'e program does not compile as 1 is being read wit'out being initialiFed* * 5'e program does not compile because o( t'e statement 1 = 6 = i = 1; public class test { 3* 5'e program compiles and runs printing <* public static void main(String args[]) { !* 5'e program compiles and runs printing 1* S2stem*out*println(args[<]4: :4args*lengt'); #* 5'e program compiles and runs printing * % % R &'at gets printed on t'e standard output w'en t'e class below is compiled and e-ecuted 1* 6ava b2 entering :6ava test lets see w'at 'appens:* * test Select t'e one correct answer* 3* lets !* 3 #* ! public class test { $* # public static void main(String args[]) { >* $ S2stem*out*println(args[<]4: :4args[args*lengt'E1]); R &'at 'appens w'en t'e (ollowing program is % compiled and run* Select t'e one correct % answer* 1* 5'e program will t'row an /rra23nde-=ut=(7ounds e-ception* * 5'e program will print :6ava test: 3* 5'e program will print :6ava 'appens:; !* 5'e program will print :test 'appens: #* 5'e program will print :lets 'appens: public class e-ample { int i = <; public static void main(String args[]) { int i = 1; i = c'angePi(i); S2stem*out*println(i); R &'at gets printed on t'e standard output % w'en t'e class below is compiled and e-ecuted public static int c'angePi(int i) { b2 entering :6ava test lets see w'at 'appens:* i= ; Select t'e one correct answer* i ;= ; return i; % public class test { % public static void main(String args[]) { S2stem*out*println(args[<]4: 1* 5'e program does not compile* :4args[args*lengt']); * 5'e program prints <* % 3* 5'e program prints 1*

!* 5'e program prints * #* 5'e program prints !*

R &'at 'appens w'en t'e (ollowing program is compiled and run* Select t'e one correct answer*

R &'at 'appens w'en t'e (ollowing program is compiled and run* Select t'e one correct answer* public class e-ample { int i[] = {<%; public static void main(String args[]) { public class e-ample { int i[] = {1%; int i = <; c'angePi(i); public static void main(String args[]) { S2stem*out*println(i[<]); int i = 1; % c'angePi(i); public static void c'angePi(int i[]) { S2stem*out*println(i); int 6[] = { %; % i = 6; public static void c'angePi(int i) { % i= ; % i ;= ; % 1* 5'e program does not compile* % * 5'e program prints <* 3* 5'e program prints 1* 1* 5'e program does not compile* !* 5'e program prints * * 5'e program prints <* #* 5'e program prints !* 3* 5'e program prints 1* !* 5'e program prints * /.S&,+S0E #* 5'e program prints !* EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE EEEEE R &'at 'appens w'en t'e (ollowing program is 1* a" b" c compiled and run* Select t'e one correct * a" c" d" e answer* 3* d" e !* a" b" c" e* 8lease note t'at strict(p is a new 1e2word in Iava * See SunHs site (or more public class e-ample { details* int i[] = {<%; #* b" c" d" e" g public static void main(String args[]) { $* /n2 o( t'e (ollowing are correct answers E int i[] = {1%; <$" <<$" or <<<$ c'angePi(i); >* /n2 o( t'e (ollowing are correct answers E S2stem*out*println(i[<]); <-<3" <U<3" <U3 or <-3 % C* /n2 o( t'e (ollowing are correct answers E public static void c'angePi(int i[]) { <-<a" <U<a" <Ua" <-a" <-</" <U</" <U/" <-/ i[<] = ; D* a i[<] ;= ; 1<* b % 11* c % 1 *b 13* d* 5'e variable 2 is getting read be(ore 1* 5'e program does not compile* being properl2 initialiFed* * 5'e program prints <* 1!* d* 3nstance variable o( t2pe int and String 3* 5'e program prints 1* are initialiFed to < and null respectivel2* !* 5'e program prints * 1#* a #* 5'e program prints !* 1$* e 1>* a" b" d" e

1* < *1 3* !* 3 !* &'ic' o( t'ese lines will compile) Select t'e (our correct answers* 1* s'ort s = <; 1* 3n t'e (ollowing class de(inition" w'ic' is t'e * b2te b = 1 C; (irst line (i( an2) t'at causes a compilation error* 3* c'ar c = 3 ; Select t'e one correct answer* !* double d = 1*!;; #* (loat ( = 1*!; $* b2te e = <; public class test { #* 5'e signed rig't s'i(t operator in Iava is EE* public static void main(String args[]) { Select t'e one correct answer* c'ar c; 1* KK; int i; * MM c = H/H; // 1 3* MMM; i = c; // !* .one o( t'ese* c = i 4 1; //3 $* &'at gets printed on t'e standard output c44; //! w'en t'e class below is compiled and % e-ecuted* Select t'e one correct answer* %

1C* b" d" e" ( 1D* d <* b 1* a" b" c *a 3* d !* e #* a $* c" e >* e C* c D* e 3<* c

class test { public static void main(String args[]) { int i"6"1"l=<; 1 = l44; 6 = 441; i = 644; S2stem*out*println(i); % %

Questions on Operator and Assignments

1* 5'e line labeled 1* * 5'e line labeled * 3* 5'e line labeled 3* !* 5'e line labeled !* #* /ll t'e lines are correct and t'e program compiles* * &'ic' o( t'ese assignments are valid* Select t'e (our correct answers* 1* s'ort s = C; * (loat ( = *3; 3* double d = *3; !* int 3 = H1H; #* b2te b = 1 ; 3* &'at gets printed w'en t'e (ollowing program is compiled and run* Select t'e one correct answer*

public class S'ortC1t { public static void main(String args[]) { int i = <; boolean t = true; boolean ( = (alse" b; b = (t VV ((i44) == <)); b = (( VV ((i4= ) M <)); S2stem*out*println(i); % % 1* < *1 3* !* 3

>* &'at gets printed on t'e standard output w'en t'e class below is compiled and e-ecuted* Select t'e one correct answer* % public class S'ortC1t { public static void main(String args[]) { int i = <; boolean t = true; boolean ( = (alse" b; b = (t V ((i44) == <)); b = (( V ((i4= ) M <)); S2stem*out*println(i); % % 1* < *1 3* !* 3 C* &'at gets printed on t'e standard output w'en t'e class below is compiled and e-ecuted* Select t'e one correct answer* public class S'ortC1t { public static void main(String args[]) { int i = <; boolean t = true; boolean ( = (alse" b; b = (t OO ((i44) == <)); b = (( OO ((i4= ) M <)); S2stem*out*println(i); % % 1* < *1 3* !* 3 D* &'at gets printed on t'e standard output w'en t'e class below is compiled and e-ecuted* Select t'e one correct answer* public class S'ortC1t { public static void main(String args[]) { int i = <; boolean t = true; boolean ( = (alse" b; b = (t O ((i44) == <));

b = (( O ((i4= ) M <)); S2stem*out*println(i); % 1* < *1 3* !* 3 1<* &'ic' operator is used to per(orm bitwise inversion in Iava* Select t'e one correct answer* 1* W *L 3* V !* O #* X 11* &'at gets printed w'en t'e (ollowing program is compiled and run* Select t'e one correct answer*

public class test { public static void main(String args[]) { b2te - = 3; - = (b2te)W-; S2stem*out*println(-); % % 1* 3 *< 3* 1 !* 11 #* # $* 1! >* 1 ! C* E! 1 * &'at gets displa2ed on t'e screen w'en t'e (ollowing program is compiled and run* Select t'e one correct answer* public class test { public static void main(String args[]) { int -"2; - = 3 V #; 2 = 3 O #; S2stem*out*println(- 4 : : 4 2);

% % 1* > 1 *3> 3* 1 > !* 3 1 #* 1 3 $* > 3 >* > # 13* &'at gets displa2ed on t'e screen w'en t'e (ollowing program is compiled and run* Select t'e one correct answer* public class test { public static void main(String args[]) { int -"2; - = 1 V >; 2 = 3 X $; S2stem*out*println(- 4 : : 4 2); % % 1* 1 3 *3# 3* # 1 !* 3 $ #* 1 > $* 1 # 1!* &'ic' operator is used to per(orm bitwise e-clusive or* 1* V *X 3* O !* L #* W 1#* &'at gets displa2ed on t'e screen w'en t'e (ollowing program is compiled and run* Select t'e one correct answer* public class test { public static void main(String args[]) { boolean - = true; int a; i((-) a = - ) 10 ; else a = - ) 30 !; S2stem*out*println(a); % % 1* 1 * 3* 3 !* ! 1$* &'at gets displa2ed on t'e screen w'en t'e (ollowing program is compiled and run* Select t'e one correct answer* public class test { public static void main(String args[]) { boolean - = (alse; int a; i((-) a = - ) 10 ; else a = - ) 30 !; S2stem*out*println(a); % % 1* 1 * 3* 3 !* ! 1>* &'at gets displa2ed on t'e screen w'en t'e (ollowing program is compiled and run* Select t'e one correct answer* public class test { public static void main(String args[]) { int -" 2; - = # MM ; 2 = - MMM ; S2stem*out*println(2); % % 1* # * 3* C< !* < #* $! 1C* &'at gets displa2ed on t'e screen w'en t'e (ollowing program is compiled and run* Select t'e one correct answer* public class test { public static void main(String args[]) {

int -; - = E3 MM 1; - = - MMM ; - = - KK 1; S2stem*out*println(-); % %

i((- M <) - = 1; switc'(-) { case 10 S2stem*out*println(1); case <0 S2stem*out*println(<); case 0 S2stem*out*println( ); brea1; case 30 S2stem*out*println(3); de(ault0 S2stem*out*println(!); brea1; %

1* 1 *< 3* > % !* # % #* 3 $* 1!>!C3$!$ 1* < 1D* &'ic' o( t'e (ollowing are correct* Select *1 all correct answers* 3* 1* Iava provides two operators to do le(t !* 3 s'i(t E KK and KKK* #* ! * MM is t'e Fero (ill rig't s'i(t operator* * &'at 'appens w'en t'e (ollowing class is 3* MMM is t'e signed rig't s'i(t operator* compiled and run* Select one correct answer* !* Nor positive numbers" results o( operators MM and MMM are same* <* &'at is t'e result o( compiling and running public class test { t'e (ollowing program* Select one correct public static void main(String args[]) { answer* int - = <" 2 = 1" F; i((-) F = <; public class test { else public static void main(String args[]) { F = 1; int i = E1; i = i MM 1; i((2) S2stem*out*println(i); F= ; % else % F = 3; S2stem*out*println(F); 1* $3 % * E1 % 3* < !* 1 1* 5'e program prints < #* 1 > * 5'e program prints 1 $* 1 C 3* 5'e program prints >* ## !* 5'e program prints 3 1* &'at all gets printed w'en t'e (ollowing #* 5'e program does not compile because gets compiled and run* Select t'e two correct o( problems in t'e i( statement* answers* 3* &'ic' all lines are part o( t'e output w'en t'e (ollowing code is compiled and run* Select t'e nine correct answers* public class e-ample { public static void main(String args[]) { int - = <; public class test {

public static void main(String args[]) { (or(int i = <; i K 3; i44) { (or(int 6 = 3; 6 M= <; 6EE) { i((i == 6) continue; S2stem*out*println(i 4 : : 4 6); % % % % 1* < < *<1 3* < !* < 3 #* 1 < $* 1 1 >* 1 C* 1 3 D* < 1<* 1 11* 1 * 3 13* 3 < 1!* 3 1 1#* 3 1$* 3 3 1>* 5'e program does not print an2t'ing* !* &'ic' all lines are part o( t'e output w'en t'e (ollowing code is compiled and run* Select t'e one correct answer*

D* < 1<* 1 11* 1 * 3 13* 3 < 1!* 3 1 1#* 3 1$* 3 3 1>* 5'e program does not print an2t'ing* #* &'ic' all lines are part o( t'e output w'en t'e (ollowing code is compiled and run* Select t'e si- correct answers* public class test { public static void main(String args[]) { (or(int i = <; i K 3; i44) { (or(int 6 = 3; 6 M= <; 6EE) { i((i == 6) brea1; S2stem*out*println(i 4 : : 4 6); % % % %

1* < < *<1 3* < !* < 3 #* 1 < $* 1 1 >* 1 public class test { C* 1 3 public static void main(String args[]) { D* < (or(int i = <; i K 3; i44) { 1<* 1 (or(int 6 = 3; 6 K= <; 6EE) { 11* i((i == 6) continue; 1 * 3 S2stem*out*println(i 4 : : 4 6); 13* 3 < % 1!* 3 1 % 1#* 3 % 1$* 3 3 % $* &'ic' all lines are part o( t'e output w'en t'e (ollowing code is compiled and run* Select 1* < < t'e si- correct answers* *<1 3* < !* < 3 public class test { #* 1 < public static void main(String args[]) { $* 1 1 outer0 (or(int i = <; i K 3; i44) { >* 1 (or(int 6 = 3; 6 M= <; 6EE) { C* 1 3 i((i == 6) continue outer;

S2stem*out*println(i 4 : : 4 6); % % % % 1* < < *<1 3* < !* < 3 #* 1 < $* 1 1 >* 1 C* 1 3 D* < 1<* 1 11* 1 * 3 13* 3 < 1!* 3 1 1#* 3 1$* 3 3 >* &'ic' all lines are part o( t'e output w'en t'e (ollowing code is compiled and run* Select t'e t'ree correct answers*

1!* 3 1 1#* 3 1$* 3 3 /nswers to ?uestions on =perators and /ssignments

1* c* 3t is not possible to assign an integer to a c'aracter in t'is case wit'out a cast* * a" c" d" e* *3 is o( t2pe double* So it cannot be assigned to a (loat wit'out a cast* 3* b !* a" c" d" (* 3( +BS (+ig't 'and side) is an integer wit'in t'e correct range o( @BS (@e(t 'and side)" and i( @BS is c'ar" b2te" or s'ort" no cast is re?uired* / decimal number is a double b2 de(ault* /ssigning it to (loat re?uires a cast* #* b $* b* 3n t'e second assignment to variable b" t'e e-pression (i4= ) does not get evaluated* >* d C* c D* d 1<* a 11* ' 1 *c public class test { 13* ( public static void main(String args[]) { 1!* b outer 0 (or(int i = <; i K 3; i44) { 1#* a (or(int 6 = 3; 6 M= <; 6EE) { 1$* d i((i == 6) brea1 outer; 1>* d S2stem*out*println(i 4 : : 4 6); 1C* ( % 1D* d % <* b % 1* a" c % * e* 5'e e-pression in t'e i( statement must evaluate to a boolean* 1* < < 3* b" c" d" e" g" '" i" 6" l *<1 !* ? 3* < #* b" c" d" g" '" l !* < 3 $* b" c" d" g" '" l #* 1 < >* b" c" d $* 1 1 >* 1 C* 1 3 D* < 1<* 1 11* 1 * 3 13* 3 <

Das könnte Ihnen auch gefallen