Java Programming Fundamentals

Test your knowledge of Java programming concepts including variables, control flow, collections (ArrayList, HashMap), iterators, and memory management.

20 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

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”?

  1. itemMap.remove("orange"); itemMap.put("orange", new Item("Orange") );
  2. ((Item)itemMap.get(“orange”)).name = “Orange”;
  3. itemMap.put("orange", new Item("Orange") );
  4. None of above
Question 2 Multiple Choice (Single Answer)

Which is the most performance efficient implementation for assignCode method from the given options?

  1. code attribute cannot be changed as Item class doesn’t have setter method for it.
  2. for (int i = 0; i < itemList.size(); i++) { if(itemList.get(i) != null) { ((Item)itemList.get(i)).code = 50+i; } }
  3. int i = 0; Item im = null; while (i < itemList.size()) { im = (Item)itemList.get(i); if(im != null) { im.code = 50+i; } i++; }
  4. int i = 0; Item im = null; Iterator itr = itemList.iterator(); while (itr.hasNext()) { im =(Item) itr.next(); im.code = 50+i; i++; }
Question 3 Multiple Choice (Single Answer)

Which of the given options code is correct and will not waste memory.

  1. Item im = new Item(null); im = (Item ) itemList.get(0);
  2. Item im = new Item(“Done”); im = (Item ) itemList.get(0);
  3. Item im = null; im = (Item ) itemList.get(0);
  4. All of the above.
Question 4 Multiple Choice (Single Answer)

Which code in the given options if put in below Main class will not print “Done”?

  1. Function call:- changeName(im); Function implementation :- { im.name = "Done"; return null; }
  2. Function call:- changeName(im); Function implementation :- { im = new Item("Done"); return null; }
  3. Function call :- im = changeName(im); function implementation :- { Item otherObject = new Item("Done"); return otherObject; }
  4. All of the above
Question 5 Multiple Choice (Single Answer)

Which code from the given options if put in Main class will not print “Done”?

  1. Function call:- changeName(im); Function implementation :- { im.name = "Done"; return null; }
  2. Function call:- changeName(im); Function implementation :- { im = new Item("Done"); return null; }
  3. Function call :- im = changeName(im); function implementation :- { Item otherObject = new Item("Done"); return otherObject; }
  4. All of the above
Question 6 Multiple Choice (Single Answer)

Which is simplest code snippet you will add in the main function to set the name attribute of 50th in the ItemList to “Done”?

  1. im.name = "Done";
  2. Item otherObject = new Item(“Done”); itemList.set(49, otherObject);
  3. ((Item)itemList.get(49)).name = “Done”;
  4. None of above is correct as they are not changing 50th element.
Question 7 Multiple Choice (Single Answer)

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.

  1. Item im = new Item(null); im = (Item ) itemList.get(0);
  2. Item im = new Item(“Done”); im = (Item ) itemList.get(0);
  3. Item im = null; im = (Item ) itemList.get(0);
  4. All of the above.
Question 8 Multiple Choice (Single Answer)

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”?

  1. itemMap.remove("orange"); itemMap.put("orange", new Item("Orange") );
  2. ((Item)itemMap.get(“orange”)).name = “Orange”;
  3. itemMap.put("orange", new Item("Orange") );
  4. None of above
Question 9 Multiple Choice (Single Answer)

Which is the most performance efficient implementation for assignCode method from the given options?

  1. code attribute cannot be changed as Item class doesn’t have setter method for it.
  2. for (int i = 0; i < itemList.size(); i++) { if(itemList.get(i) != null) { ((Item)itemList.get(i)).code = 50+i; } }
  3. int i = 0; Item im = null; while (i < itemList.size()) { im = (Item)itemList.get(i); if(im != null) { im.code = 50+i; } i++; }
  4. int i = 0; Item im = null; Iterator itr = itemList.iterator(); while (itr.hasNext()) { im =(Item) itr.next(); im.code = 50+i; i++; }
Question 10 Multiple Choice (Single Answer)

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”?

  1. itemMap.remove("orange"); itemMap.put("orange", new Item("Orange") );
  2. ((Item)itemMap.get(“orange”)).name = “Orange”;
  3. itemMap.put("orange", new Item("Orange") );
  4. None of above
Question 11 Multiple Choice (Single Answer)

Which is the most performance efficient implementation for assignCode method from the given options?

  1. code attribute cannot be changed as Item class doesn’t have setter method for it.
  2. for (int i = 0; i < itemList.size(); i++) { if(itemList.get(i) != null) { ((Item)itemList.get(i)).code = 50+i; } }
  3. int i = 0; Item im = null; while (i < itemList.size()) { im = (Item)itemList.get(i); if(im != null) { im.code = 50+i; } i++; }
  4. int i = 0; Item im = null; Iterator itr = itemList.iterator(); while (itr.hasNext()) { im =(Item) itr.next(); im.code = 50+i; i++; }
Question 12 Multiple Choice (Single Answer)

Which code from the given options if put in Main class will not print “Done”?

  1. Function call:- changeName(im); Function implementation :- { im.name = "Done"; return null; }
  2. Function call:- changeName(im); Function implementation :- { im = new Item("Done"); return null; }
  3. Function call :- im = changeName(im); function implementation :- { Item otherObject = new Item("Done"); return otherObject; }
  4. All of the above
Question 13 Multiple Choice (Single Answer)

Which is simplest code snippet you will add in the main function to set the name attribute of 50th in the ItemList to “Done”?

  1. im.name = "Done";
  2. Item otherObject = new Item(“Done”); itemList.set(49, otherObject);
  3. ((Item)itemList.get(49)).name = “Done”;
  4. None of above is correct as they are not changing 50th element.
Question 14 Multiple Choice (Single Answer)

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.

  1. Item im = new Item(null); im = (Item ) itemList.get(0);
  2. Item im = new Item(“Done”); im = (Item ) itemList.get(0);
  3. Item im = null; im = (Item ) itemList.get(0);
  4. All of the above.
Question 15 Multiple Choice (Single Answer)

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?

  1. a) V_SUM
  2. b) CALC_ORD.
  3. c) FORWARD_PACK
  4. d) GENERATE_SUMMARY.
Question 16 Multiple Choice (Single Answer)

Which of the following is not a java keyword?

  1. assert
  2. interface
  3. extend
  4. transient
Question 17 Multiple Choice (Single Answer)

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++; } }

  1. x=1 and y=1
  2. x=1 and y=2
  3. x=2 and y=2
  4. x=2 and y=1
Question 18 Multiple Choice (Single Answer)

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++; } }

  1. x=1 and y=1
  2. x=2 and y=2
  3. x=1 and y=2
  4. x=2 and y=1
Question 19 Multiple Choice (Single Answer)

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); } }

  1. 10
  2. 0
  3. Garbage Value
  4. Compile time error
Question 20 Multiple Choice (Single Answer)

How many times pre query and post query fired when we fetch 10 record from a table ?

  1. 1,1
  2. 1,10
  3. 10,10
  4. 10,1