Java Programming with Database Connectivity
Test your knowledge of Java programming concepts including JDBC, collections, object-oriented programming, operators, control flow, exceptions, arrays, and generics.
Questions
import java.util.Scanner; public class ScannerTest { public static void main(String[] args) { Scanner scanner = new Scanner("hello 1 2.00 false"); scanner.useDelimiter(" "); String str = scanner.next(); int anInt = scanner.nextInt(); float aFloat = scanner.nextFloat(); boolean booleanValue = scanner.nextBoolean(); System.out.println(str + ":" + anInt + ":" + aFloat + ":" + booleanValue); } }
- The program will output 'hello:1:2.0:false'
- The program will throw Input Mismatch exception at the run-time
- The program will output nothing.
- The program will ouput ':1:2.0:false'
public class Gen<G>{ G g; Gen(G g) { this.g =g; } public static void main(String[] args) { Gen<String> arr[] = new Gen[5]; //line 1 arr[0] = new Gen("Java"); //line 2 arr[1] = new Gen(1); //line 3 arr[2] = (Gen<String>)new Gen(1); //line 4 arr[3] = (Gen<String>)new Gen<Integer>(1); //line 5 for(Gen o:arr) { System.out.println(o); } } }
- Compile time Error at line 1
- Compile time Error at line 3
- Compile time Error at line 4
- Compile time Error at line 5
- Run Time Error
What will be printed out if you attempt to compile and run the following code? int i=9; switch (i) { default: System.out.println("default"); case 0: System.out.println("zero"); break; case 1: System.out.println("one"); case 2: System.out.println("two"); }
- default
- default, zero
- error default clause not defined
- no output displayed
Which of the following statements are true?
- Methods cannot be overriden to be more private
- static methods cannot be overloaded
- private methods cannot be overloaded
- An overloaded method cannot throw exceptions not checked in the base class
What will be printed out if this code is run with the following command line? java myprog good morning public class myprog{ public static void main(String argv[]) { System.out.println(argv[2]); } }
- myprog
- good
- morning
- Exception raised: "java.lang.ArrayIndexOutOfBoundsException: 2"
Which of the following is not keyword or reserved word in Java?
- if
- then
- goto
- while
- case
What will happen if you try to compile and run the following code? public class Q { public static void main(String argv[]){ int anar[]=new int[5]; System.out.println(anar[0]); } }
- Error: anar is referenced before it is initialized
- null
- 0
- 5
What will be the result of attempting to compile and run the following code? abstract class MineBase { abstract void amethod(); static int i; } public class Mine extends MineBase { public static void main(String argv[]){ int[] ar=new int[5]; for(i=0;i < ar.length;i++) System.out.println(ar[i]); } }
- a sequence of 5 0's will be printed
- Error: ar is used before it is initialized
- Error Mine must be declared abstract
- IndexOutOfBoundes Error
Which of the following lines of code will compile without error? a) int i=0; if(i) { System.out.println("Hello"); } b) boolean b=true; boolean b2=true; if(b==b2) { System.out.println("So true"); } c) int i=1; int j=2; if(i==1 &| j==2) System.out.println("OK");
- a)
- b)
- c)
- none
What will be output if you try to compile and run the following code, but there is no file called Hello.txt in the current directory? import java.io.*; public class Mine { public static void main(String argv[]){ Mine m=new Mine(); System.out.println(m.amethod()); } public int amethod() { try { FileInputStream dis=new FileInputStream("Hello.txt"); }catch (FileNotFoundException fne) { System.out.println("No such file found"); return -1; }catch(IOException ioe) { } finally{ System.out.println("Doing finally"); } return 0; } }
- No such file found
- No such file found ,-1
- No such file found, Doing finally, -1
- 0
What will happen if you attempt to compile and run the following code? class Base {} class Sub extends Base {} class Sub2 extends Base {} public class CEx{ public static void main(String argv[]){ Base b=new Base(); Sub s=(Sub) b; } }
- Compile and run without error
- Compile time Exception
- Runtime Exception
- none
What is the result of the following operation? System.out.println(4 | 3);
- 6
- 0
- 1
- 7
Given the following code how could you invoke the Base constructor that will print out the string "base constructor"; class Base{ Base(int i){ System.out.println("base constructor"); } Base(){ } } public class Sup extends Base{ public static void main(String argv[]){ Sup s= new Sup(); //One } Sup() { //Two } public void derived() { //Three } }
- On the line After //One put Base(10);
- On the line After //One put super(10);
- On the line After //Two put super(10);
- On the line After //Three put super(10);
You need to create a class that will store unique object elements. You do not need to sort these elements but they must be unique. What interface might be most suitable to meet this need?
- Set
- List
- Map
- Vector
There is a table called Employee having columns are empno, ename , job , deptno,grade and salary. Which of the following is a correct query to change all emp salary with salary<10000 to 1.5 times and grade to ‘Promo’
- Update Employee set salary = salary * 1.5 and grade=’Promo’ where salary < 10000;
- Update Employee set salary = salary * 1.5, grade=’Promo’ where salary < 10000;
- Update table Employee set salary = salary * 1.5, grade=’Promo’ where salary < 10000;
- Update table Employee set salary = salary * 1.5 and grade=’Promo’ where salary < 10000;
Which of the following is Data Definition Language?
- INSERT
- DELETE
- UPDATE
- CREATE
Which type of Statement can execute parameterized queries?
- PreparedStatement
- ParameterizedStatement
- ParameterizedStatement and CallableStatement
- All kinds of Statements (i.e. which implement a sub interface of Statement)
Which type of Statement can execute queries stored on db server?
- CallableStatement
- ParameterizedStatement
- ParameterizedStatement and CallableStatement
- All kinds of Statements (i.e. which implement a sub interface of Statement)
Which packages contain the JDBC classes?
- java.jdbc and javax.jdbc
- java.jdbc and java.jdbc.sql
- java.sql and javax.sql
- java.rdb and javax.rdb