Java Programming Fundamentals
Test your knowledge of Java programming concepts including variables, control flow, collections (ArrayList, HashMap), iterators, and memory management.
Questions
Which is simplest and correct code snippet needs to be put in the main function to replace the value object of the HashMap for the key “orange”?
- itemMap.remove("orange"); itemMap.put("orange", new Item("Orange") );
- ((Item)itemMap.get(“orange”)).name = “Orange”;
- itemMap.put("orange", new Item("Orange") );
- None of above
Which is the most performance efficient implementation for assignCode method from the given options?
- code attribute cannot be changed as Item class doesn’t have setter method for it.
- for (int i = 0; i < itemList.size(); i++) { if(itemList.get(i) != null) { ((Item)itemList.get(i)).code = 50+i; } }
- int i = 0; Item im = null; while (i < itemList.size()) { im = (Item)itemList.get(i); if(im != null) { im.code = 50+i; } i++; }
- int i = 0; Item im = null; Iterator itr = itemList.iterator(); while (itr.hasNext()) { im =(Item) itr.next(); im.code = 50+i; i++; }
Which of the given options code is correct and will not waste memory.
- Item im = new Item(null); im = (Item ) itemList.get(0);
- Item im = new Item(“Done”); im = (Item ) itemList.get(0);
- Item im = null; im = (Item ) itemList.get(0);
- All of the above.
Which code in the given options if put in below Main class will not print “Done”?
- Function call:- changeName(im); Function implementation :- { im.name = "Done"; return null; }
- Function call:- changeName(im); Function implementation :- { im = new Item("Done"); return null; }
- Function call :- im = changeName(im); function implementation :- { Item otherObject = new Item("Done"); return otherObject; }
- All of the above
Which code from the given options if put in Main class will not print “Done”?
- Function call:- changeName(im); Function implementation :- { im.name = "Done"; return null; }
- Function call:- changeName(im); Function implementation :- { im = new Item("Done"); return null; }
- Function call :- im = changeName(im); function implementation :- { Item otherObject = new Item("Done"); return otherObject; }
- All of the above
Which is simplest code snippet you will add in the main function to set the name attribute of 50th in the ItemList to “Done”?
- im.name = "Done";
- Item otherObject = new Item(“Done”); itemList.set(49, otherObject);
- ((Item)itemList.get(49)).name = “Done”;
- None of above is correct as they are not changing 50th element.
Which of the given options code is correct and will not waste memory. Note :- Defination of the Class Item is class Item { public int code; public String name; public Item(String name) { this.name = name; } } itemList is ArrayList containing 50 objects of Class Items.
- Item im = new Item(null); im = (Item ) itemList.get(0);
- Item im = new Item(“Done”); im = (Item ) itemList.get(0);
- Item im = null; im = (Item ) itemList.get(0);
- All of the above.
Which is simplest and correct code snippet needs to be put in the main function to replace the value object of the HashMap for the key “orange”?
- itemMap.remove("orange"); itemMap.put("orange", new Item("Orange") );
- ((Item)itemMap.get(“orange”)).name = “Orange”;
- itemMap.put("orange", new Item("Orange") );
- None of above
Which is the most performance efficient implementation for assignCode method from the given options?
- code attribute cannot be changed as Item class doesn’t have setter method for it.
- for (int i = 0; i < itemList.size(); i++) { if(itemList.get(i) != null) { ((Item)itemList.get(i)).code = 50+i; } }
- int i = 0; Item im = null; while (i < itemList.size()) { im = (Item)itemList.get(i); if(im != null) { im.code = 50+i; } i++; }
- int i = 0; Item im = null; Iterator itr = itemList.iterator(); while (itr.hasNext()) { im =(Item) itr.next(); im.code = 50+i; i++; }
Which is simplest and correct code snippet needs to be put in the main function to replace the value object of the HashMap for the key “orange”?
- itemMap.remove("orange"); itemMap.put("orange", new Item("Orange") );
- ((Item)itemMap.get(“orange”)).name = “Orange”;
- itemMap.put("orange", new Item("Orange") );
- None of above
Which is the most performance efficient implementation for assignCode method from the given options?
- code attribute cannot be changed as Item class doesn’t have setter method for it.
- for (int i = 0; i < itemList.size(); i++) { if(itemList.get(i) != null) { ((Item)itemList.get(i)).code = 50+i; } }
- int i = 0; Item im = null; while (i < itemList.size()) { im = (Item)itemList.get(i); if(im != null) { im.code = 50+i; } i++; }
- int i = 0; Item im = null; Iterator itr = itemList.iterator(); while (itr.hasNext()) { im =(Item) itr.next(); im.code = 50+i; i++; }
Which code from the given options if put in Main class will not print “Done”?
- Function call:- changeName(im); Function implementation :- { im.name = "Done"; return null; }
- Function call:- changeName(im); Function implementation :- { im = new Item("Done"); return null; }
- Function call :- im = changeName(im); function implementation :- { Item otherObject = new Item("Done"); return otherObject; }
- All of the above
Which is simplest code snippet you will add in the main function to set the name attribute of 50th in the ItemList to “Done”?
- im.name = "Done";
- Item otherObject = new Item(“Done”); itemList.set(49, otherObject);
- ((Item)itemList.get(49)).name = “Done”;
- None of above is correct as they are not changing 50th element.
Which of the given options code is correct and will not waste memory. Note :- Defination of the Class Item is class Item { public int code; public String name; public Item(String name) { this.name = name; } } itemList is ArrayList containing 50 objects of Class Items.
- Item im = new Item(null); im = (Item ) itemList.get(0);
- Item im = new Item(“Done”); im = (Item ) itemList.get(0);
- Item im = null; im = (Item ) itemList.get(0);
- All of the above.
Examine this package body: CREATE OR REPLACE PACKAGE BODY forward_pack IS V_sum NUMBER; - 44 - PROCEDURE calc_ord(. . . ); PROCEDURE generate_summary(. . . ) IS BEGIN Calc_ord(. . . ); . . . END calc_ord; END forward_pack; / Which construct has a forward declaration?
- a) V_SUM
- b) CALC_ORD.
- c) FORWARD_PACK
- d) GENERATE_SUMMARY.
Which of the following is not a java keyword?
- assert
- interface
- extend
- transient
which is the value of x and y after running this code? class a { public static void main(String args[]) { int x=1; int y=x++; } }
- x=1 and y=1
- x=1 and y=2
- x=2 and y=2
- x=2 and y=1
what is the value of x and y after running this code? class a { public static void main(String args[]) { int x=1; int y=x++; } }
- x=1 and y=1
- x=2 and y=2
- x=1 and y=2
- x=2 and y=1
What is the output:class a { public static void main (String args[]) { int x; int y=10; if (y>0) { x = 10; } System.out.println(x); } }
- 10
- 0
- Garbage Value
- Compile time error
How many times pre query and post query fired when we fetch 10 record from a table ?
- 1,1
- 1,10
- 10,10
- 10,1