Java Fundamentals

 

Java  Fundamental

Q1. Which of the following are valid definitions of an application’s main( ) method?
a) public static void main();
b) public static void main( String args );
c) public static void main( String args[] );
d) public static void main( Graphics g );
e) public static boolean main( String args[] );

 

2. If MyProg.java were compiled as an
application and then run from the command line as:

   java MyProg I like tests

what would be the value of args[ 1 ] inside
the main( ) method?
a) MyProg
b) "I"
c) "like"
d) 3
e) 4
f) null until a value is assigned

 

3. Consider this class example:

class MyPoint 
{ 
 void myMethod() 
   {  
      int x, y;
      x = 5; y = 3;
      System.out.print( " ( " + x + ", " + y + " ) " );
      switchCoords( x, y ); ///PASS BY VALUE SO NO CHANGE IN MAIN 
      System.out.print( " ( " + x + ", " + y + " ) " );
   }
   void switchCoords( int x, int y ) 
   {  int temp;
      temp = x;
      x = y;
      y = temp;
      System.out.print( " ( " + x + ", " + y + " ) " );
   }
}

What is printed to standard output if myMethod()
is executed?
a) (5, 3) (5, 3) (5, 3)
b) (5, 3) (3, 5) (3, 5)
c) (5, 3) (3, 5) (5, 3)

4. Consider the code below:

public static void main( String args[] )
{  int a = 5;
   System.out.println( cube( a ) );
}
int cube( int theNum )  ///CUBE IS NON STATIC…TRY…CREATE A CLASS USE CUBE() IN THAT CLASS…CREATE OBJECT OF THAT CLASS…NOW TRY IT..
{
   return theNum * theNum * theNum;
}
 

What will happen when you attempt to compile and run this code?
a) It will not compile because cube is
already defined in the java.lang.Math class.
b) It will not compile because cube is
not static.

c) It will compile, but throw an
arithmetic exception.
d) It will run perfectly and print
"125" to standard output.

5. If val = 1 in the code below:

 
switch( val ) 
{  case 1: System.out.print( "P" );
   case 2: 
   case 3: System.out.print( "Q" );
      break;
   case 4: System.out.print( "R" );
   default: System.out.print( "S" );
}
 

Which values would be printed?
a) P
b) Q

c) R
d) S

6. What is the parameter specification for the public static void main method?

  1. String args []
  2. String [] args
  3. Strings args []
  4. String args

Select all correct answers.

7. What will be the result of compiling the following code:

 

public class Test {

public static void main (String args []) {

int age;  ///LOCAL VARIABLE///NEEDS 2 B INITIALIED

age = age + 1;

System.out.println("The age is " + age);

}

} 

  1. Compiles and runs with no output
  2. Compiles and runs printing out The age is 1
  3. Compiles but generates a runtime error
  4. Does not compile

 

8. Which of the following is illegal:

  1. int i = 32;
  2. float f = 45.0;
  3. double d = 45.0;

9. What will be the result of compiling the following code:

 

public class Test {

static int age;

public static void main (String args []) {

age = age + 1;

System.out.println("The age is " + age);

}

} 

  1. Compiles and runs with no output
  2. Compiles and runs printing out The age is 1
  3. Compiles but generates a runtime error
  4. Does not compile
  5. Compiles but generates a compile time error

 

10. Which of the following are so called "short circuit" logical operators?

  1. &
  2. ||
  3. &&
  4. |