You are on page 1of 47
1 marks) ‘Which ofthe following statements is TRUE with respect to Java language being platform independent? © Te code in java files platform dependent © The NMisthe same across all operating systems ‘A Java program writen in @ machine with windows operating system cannot be executed on a machine having Linus operating system thoug O instates accordingly © Alas fle can be run in any operating system where Java is installed k Mucsuun c [2 marks] What is the output of the code given below? class LoopTester { public static void main(String[] args) { int endNum; for(endNum=1; endNum<=10; endNum++){ if(endNum>5){ continue: ) } for(int startNum = endNum--; ‘System.out.print(startNum+" “+endNum+" if(endNum<8){ break: :startNum rear); 23, 45, 6, 17, 15 What will be the contents of inQueue when the following operations are done? inQueue.enqueue(43); inQueuedequeuet): inQueue enqueue(12); inQueue.dequeue(): O© inQueueliront->reans 23, 45, 6.17.15 QE inQucuetfront->reari: 6.17, 15.43.12 CE imQueue(front-»rear}: 12, 43. 23. 45,6 oO inQueuelfront-> rear): 23, 45, 6, 43,12 inrrayDeque(front->rear) [1.2.3 4,§) mehugh eee InQueue(front->rean: "Apple" "Bat' Cotton” Desert" English") What will be the content of the outStack(top-> bottom) if imArrayDaque and inQueue are passed as parameters to the below metho? Public Stack convertToStack(ArrayDeque inArrayDeque, Queue inQueue}{ List interList = new Arraylist<>Q; Stack outStack = new Stack(S} do( interList.add(inArrayDeque.remove0 + inrrayDeque size) while(inArrayDeque,poll0!=null): k for{int numinterList)[ if(inQueveistmpty0){ String str = inQueve.dequeued: outStack push(num+strlength0) Youre being procte else ‘outStack push(num tack display coutStack.display(: ‘ retum outStack: Note: ‘Steck’ and ‘Queue’ refer to the user defined classes discussed as a part of the course Assumption: Queue and Stack classes, with alffts methods having necessary datatypes. are available. O© outstackttop->bottomyi{11, 8, 10] CO Exeption thrown: jave.uti, NoSuchElementException QO dutstack(top->bottom}:{10 8.11] O owtstack(tep->bottom}l7, 8.14] [1 marks] Consider the below code: class ExceptionExample { public void checkForExceptions(int num1,int num2){ int intArr}=(1.2.3) String str=null; uy str.charAt(O); System.out.printin(num1/num2); System out.printin(“Enjoy no exception! ) catch(ArithmeticException e){ // Line! Systemout.printin(“ArithmeticException handier!"} } catch(NuliPointerException e){ // Line2 System.out.printin(”NullPointerException handler!"); catchiException e){ // Line3 System.out printin( Default exception handter!™); Question 8 } class Tester{ public static void main(String] args) ExceptionExample exceptionExample=new ExceptionExampleQ: exception Example.checkForExceptions(2.0); } * 1 Which of the below catch block(s) will get executed? a, catch block placed at Line1 b. catch block placed at Line2 ¢. catch block placed at Line3 Note: Line numbers are for reference only 2 Oo Onlya © one © One ‘ © Both band c [2 marks} | Consider a queue of strings InQueve implemented using the nbult ArmayDeque class inQuewe contains the following elements: Imaueuettiont->*ear["Goorge", Lary’, "Martin’. "Mets", Karen’, “Caley What wil be the content of eutArrayDeque((ront -> rear) if InQueue is passed as an input parameter tothe below method? Public static ArrayDeque convertStructure(ArrayDeque inueve) { ArrayDequecinteger> outArrayDeque = new ArrayDeque<>(: while(inQueueismpty0) { String str = inQueve removed; outArrayDeque pushistr.length0), ifftr.Jength0!=inQueue.removed length) ( outArrayDeque offer(inQueue.removed.length)}: ) 1 ifloutArrayDeque,peck0 >5) { i 4; outArrayDeque remove) é } fig ig You're be return cutArrayDeque; 1 yy ! ) if(outArrayDeque.peek()> 5) { outArrayDeque.remove(): 1 return outArrayDeque: Oo outArrayDeque(front -> rear): [7, 6, 6] CO outArrayDequetfiont -> rear) (7, 6.6.5] OQ outArrayDeque(front -> rear): [6, 6, 5] oO outArrayDeque(front -> rear): [6, 6, 7] Question 10 [2 marks} ‘What is the output of the following code snippet? class Patient{ private int patientid; public Patient(int patientid)( this.patientld=patientld } + public int identifyAmt(double{] amountArr, int{] patientdAr){ poy id double amt=0.0; fr int counter=0; for(int index=0:index amt){ counter++ Peper tere return counter; L ! class Demo{ public static void main(String [J args) . double[] amountArr = (12,13,16,16,19.20,10.9}; int) patientidArr=(1002, 1004,1001,1003,1005,1009, ror soo Patient patientObj=new Patient(1001); - System.outprintin(patientObj.identifyAmt(amountArr. } : ) O2 Oo: © Runtime Error: Array index out of bounds © Compilation Errer: Unreachable code oo 1H mars] ohn is tying to write a regular expression for searching a string which should consist of only lowercase and uppercase letters and must end with letter ‘sowercase). Peter and Smith. teammates of John suggest few expressions. deni the suggestions) which would produce the desired output 1. Peter suggested to write as 5((ia-}°S) 2, Smith suggested to write it as *((i1A-Z1"s) O Oni Peter © Ory smitn © Beth Peter and Smith ‘Youre being proctored! © None of the suggestions —_ [2 marks] What is the output of the code given below? class Toy( 4 | protected int toyld: i protected float price: public Toy0 { this.toyld=3001; this.price=100.0F } protected void displayDetails(){ System.out println("Toy Id:"+this.toyld +* Price:"+this.price); } i public class SoftToy extends Toy( a oD) Private int discount: . - b SoftToy(int discount. float price){ ? this.toyid ++; this price=price: this.discount=discount: , \ public void caiculateTotalPrice(){ this.price=this.price- (this,price"(float)this.discount)/100; ae i public void caloulateTotalPrice 0 this.price=this.price-(this.price*(float)this.discount)/100; ) public static void main(String args) Toy car=new Toy0: car.displayDetails(: SoftToy teddyBear=new SoftToy(10.300); teddyBear.calculateTotalPrice); teddyBear.displayDetails0; Toy 14:3001 Price:1000 Toy 10:3002 Price:2700 Toy 1d:3001 Price:100.0 Toy 1d:3001 Price:270.0 Toy Id:3002 Price:100.0 Toy 1d:3002 Price:270.0 Toy 14:3001 Price:300.0 Toy 14:3002 Price:270.0 Question 13 [2 marks} Consider the following function reverseLinkedList, which is supposed to reverse a linked list containing unique elements: Public LinkedList reverselinkedList(LinkedList originalList){ LinkedList reverseList=new LinkedList(); Node temp2=originalList.getTaiiQ; int count while(temp!!=nulf( count+=1: & tempt =tempi.getNext( ’ while(count> 0) temp2=originallist. get Tall) /flinet “Hed count ) You're oniginallist=reversetist return original Wot the following options must be added at Line! and Line2 so that the code performs correctly? return onginallist ) Which of the following options must be added at Line and Line? so that the code performs correctly? Assumption: LinkedList class, with the necessary methods, is available Notes: . * Line numbers are only for reference © ‘LinkedList” refers to the user defined class discussed as a part of the course Line 1: reverseList.addAtEnd(temp2.getData0): O Line 2: originalistdeleteltemp2.getData0) Line 1: originalList.addAtEnd(temp2.getData()) 0 ine 2 reverseList.delete(temp2.getDatal)): Line 1: reverseList.addAtEnd(temp2): oO Line 2: oniginallist.delete(temp2) Line 1: originallistaddAténd(temp2): Line 2: reverseList.delete(temp2): Question 14 [2 marks} What is the output of the code given below? abstract class Demo { Public int num: Demof num = 10; } abstract public void setNum0: abstract final public void getNum0; class Test extends Demo { public void setNum(int num){ this.num = num: 1 final public void getNumn()( System.out printin(‘num =~ + num): ) ‘ — public static void main(Stringf] args){ . Test obj, = new Test(}: class Test extends Demo i public void setNum(int num} this.num = num; } final public vord getNumO{ System.out.printin(“num = * + num): J public static void main(String[] args)[ Test obj = new TestQ; obj.setNum(30): obj getNum0): } } ‘ Oo 10 ‘ ° On \ oO 30 “i . oO Compilation Error ; final method can't be overridden 5 << ~ Question 15 (2 marks} ‘What is the output of the code given below? t class Student( public String stdName; public int stdld: Public String stdBranch: public Student } ) r public Student(String stdName. int studentid. String stdBranch)( this stdName=stdName: stdid=studentid: stdBranch=stdBranch, this.stdName=stdName: stdid=studentid: stdBranch=stdBranch; class Demo public static void main(String args{)){ Student std1 = new Student(; ‘System.out.printin(“stdName:" +std1.stdName+”, "+"stdBranch:"+std1.stdBranch+”, “+"stdid:"+std1.stdld); I Student std2 = new Student(“John",1234,"CSI Student std2 = new Student("John",1234,"CSE"}, ‘System.outprintin("stdName:*+std2.stdName+", *+*stdBranch:*+std2.stdBranch+", *+"stdid:"+std2.stdid); stdName: null, stdBranch: null, stad: 0 Oo stdName: John, stdBranch: null, stdld: 1234 stdName: null, stdBranch: null, stdld: 0 oO stdName: John, stdBranch: null, stdld: 0 stdName: null, stdBranch: null, std: O 9 stdName: null, stdBraneh: null, stdid: 0 stdName: null, stdBranch: null, stdid: 0 oO stdName: John, stdBranch: CSE, stdid:1234 = [1 marks} Consider the following code that depicts BINARY SEARCH algorithm for the list of elements sorted in ASCENDING ORDER. public static int search(int arrayOfElements!].int low,int highint elementToBeSearched){ if (low <= high) ( int mid = low + high) / 2: if (arrayOfElements[mid)} return mid: if (arrayOfElements{mid] < elementToBeSearched) return search(arrayOfElements, mid + 1, high, elementToBeSearched); return search(arrayOfElements, low, mid - 1, elementToBeSearched); elementToBeSearched) 1 ~ retum-t; ) ~ —— =a Consider the arrayOfElements having elements with lew as 0 and high as 7. The elements of the array are as follows. 12345678 —— — \ ~ - 7 . XS How many iterations will be required to search if the ementTobeStrchedis 67 * > s% ent — "= — oP ~~. le ee Sy ~—. << —e oO: ==. ae jontsfenid] = = elementToBeSearched) return mid: if (arrayOfElementsimid] < element loBeSearched) return search(arrayOflements, mid + 1, high, elementToBeSearched); return search(arrayOfElements, low, mid - 1, elementToBeSearched); ’ return -1; ’ Consider the arrayOfElements having & elements with fow as 0 and high as 7. The elements of the array are as follows. 12345678 How many iterations will be required to search if the elementTeBeSearched is 6? . i ©! a at Ai es / es } » yp) \ O3 Se Ee Question 17 (2 marks} ‘Consider the following ArrayList inArrayList consisting of Integers: inArrayList: (10, 20, 30, 40, 50, 60, 70, 80) : ‘What will be the content of outList if inArrayList is passed as a parameter to the below method? public static Arraylist getModifiedList(ArrayListcinteger> inArrayList){ Arraylistcinteger> outList = new Arraylist<>0: Arraylisttemplist = new ArrayList<>(); for(int index=0;index iter = tempistlistiterator): while(iterhasNextO)( outList.add{iter.next()}: iffiter.nextindent)%3==0)( = '’ , 4 " , 4 iterremovet ee J 4 i = Ot! Pie ee _ etre iternextd: ; ) OutList.addAll(tempList) - SS Listiterator Wer = tempList.stiterator(e —— et while(iter.hasNext()( outList.add(iter.next)): ifGiter.nextindex)%3==0}{ iterremove(: } } outListaddAll(tempList); return outList; } oO outList; [10, 30, 60, BO] Oo outList: [10, 60] k i outList: [10, 60, 10, 30. 80] f al - O Runtime Exception: ConcurrentModifcationException © dl oe Question 18 (1 marks} Which of the following statements is FALSE? ‘CQ Final instance variables can be initialized in the constructor of the class ‘© Trere is no need to import classes which are present in the same package CO A arent reference cannot hold a child object © “lassA in package? can call method! defined in a public ClassB present in package? only if ClassA imports Clasc® Es vate [1 marks] Consider the code given below: class Pet ( public String preferradFood: public Pet(String preferredFood){ this.preferredFood=preterredFood: ) public void eatsFood({ //Line 1 MuspuurT 17 System.out.printin("Pet eats the desired food: “+this,preferredFood): 1 public void eatsFood(String food){ //Line 2 System.outprintin(Pet eats the desired food: "+food t public static void main(Stringl] args) { Pet cat= new Pet('Fish”); } } "7 Which of the following method calls will call the method eine tin oF D> a. CateatsFood(); b. cateatsFood(" Milk”); ©. cateatsFood("Curd"}; oO Only a oO Only band c Only c = h oO Alla, band c ‘Question 2U (1 marks] Consider the code given below: abstract class MobilePhone ( private String brand: MobilePhone(String brand) { this.brand = brand: t abstract void performTask0; x void displayFeatures() { ‘System.out printin("Brand: "+ this.brand); ' } class SmartPhone extends MobilePhone { private boolean isWifiConnectionEnabled: ‘SmartPhone(String brand. boolean isWifiConnectionEnabled) ( super(brand): thisisWifiConnectionEnabled = isWifiConnectionEnabled:. ) void performTask0) ( System.out.printin("Connect to internet!"): 1 void perform TaskQ { System.out.printinCConnect to internet!"}; ! void displayfeatures() { super.displayFeatures); System.out printin(‘Wifi Enabled: "+this.isWifiConmectionEnabled): } ) Identify the OOP principles implemented in the given code. 1. Encapsulation 2. Abstraction 3. Inheritance 4, Polymorphism Oo Only 1, 2 and 3 Q Only 1.3. anda Oo Only 1. 2 and 4 Question 21 {1 marks} Identify the order of evaluation of the expression given below : float expAnswer = num1*num2+num3%(num2-num1)-num2; i. num2-num4 fi, num3%6(num2-numt) in, num 1num2 k tv, (num2-num4)-num2 vy. num "num2+num3 Vi. num 1 num2+num3%(num2-num1) vit, num #*num2+num3%{num2-num4)-num2 Oo Ai, Wi, wi oO tt Hl, Wi vid th jnume-nums fi, num3%(num2-num1) iii, num1*num2 iv, (num2-num1)-num2 v. numt*num2+num3 vi. num #num2+num3%{num2-num1) vii, num t*num2+ num3%(num2-num1)-num2 O kit ws iv. wi CO Witt vit Question 22 13 marks} Consider the following code snippet, class Demot static String reverseString(String strValue) { /(Code to be written, ) public static void main(String [] args){ ‘String stringOne = “Infosys”: String stringRey = reverseString(stringOne): System.out.printin(*The reversed string is:" + stringRev): ) ' \ x What should be written in the reverseString method so that the output is "The reversed string is: sysofnI"? if (strValue.isEmpty0) return strValue; return reverseString(strValue.substring(1}) #strValue.charAt(O}; it (strVatue.isémpty0) Quin stevalue: ‘System.out prntin@ The reversed string is” ¥ stringRev); ) 1 What should be written in the reverseString method so that the output is "The reversed string is: sysofnI"? if (strValue.isEmptyQ) return strValue; return reverseString(strValue.substring(1))+strValue.charAt(O): if (strValueJisEmpty0) return strValue; return reverseString(strValue.substring(0)}+strValue.charAt(1); if (strValue.equals(" *)) retum strValue: return reverseString(strValue.substring(1)) +strValue.charAt{(O); if (strVatue.isEmpty0) return strValuesubstring(1): return reverseString(str'Value.substring(1))+strValue.charAt(Q); a ————Qrertion 2 [1 marks) Consider the problem size as ‘a’. What is the worst case complexity of the algorithm given below? if num > 100 then for{countert =Ocounter! 9999) ( System.out.printin( Invalid customer id"); 1 else{ System.out.printin(Valid customer id"); 4 ) 1 Which of the following programming flaws can be identified by the PMD tool? ) public void ValidateCustomarta) [ iffthis.custid >9999) { System.out.printin("inwalid customer id”); 1 ebsel System.outprintin€'Valid customer id"): ’ ' ) Which of the fotlowing programming flaws can be identified by the PMD tool? » (© Unused variable ‘custName! | © ‘alia method naming convention EO Use of Systemoutprinting CO Allthe given options

You might also like