Java and C Programming Fundamentals
Test your knowledge of Java and C programming concepts including switch statements, exception handling, loops, conditional logic, and language-specific syntax rules.
Questions
package exceptions; public class TestException5 { static String trimString(String x) { return x.trim(); } public static void main(String[] args) { try { String s = trimString(null); System.out.println("s = "+s); } catch(Exception e) { System.out.println(" Exception "); } catch(NullPointerException ne) { System.out.println("Null Pointer Exception "); } } }
- s = tcs Exception
- s = tcs Null Pointer Exception
- Exception
- Compiler Error
package exceptions; public class TestException2 { static double testException(int x, int y) { return (x/y); } public static void main(String[] args) { try { double p = testException(10,5); System.out.println("p = "+p); } catch(Exception e) { System.out.println("Exception happened "+e.getMessage()); return; } finally { System.out.println("In finally"); } } }
- p = 2.0
- Compiler Error
- Exception
- p = 2.0 In finally
package exceptions; public class TestException3 { static double testException(int x, int y) { return (x/y); } public static void main(String[] args) { try { double p = testException(10,0); System.out.println("p = "+p); } catch(Exception e) { System.out.println("Exception happened "+e.getMessage()); System.exit(0); } finally { System.out.println("In finally"); } } }
- Compiler Error
- Exception happened / by zero
- Exception happened / by zero In finally
- In finally
package exceptions; public class TestException1 { static double testException(int x, int y) { return (x/y); } public static void main(String[] args) { try { double p = testException(10,0); System.out.println("p = "+p); System.out.println("Division done"); } catch(Exception e) { System.out.println("Exception happened "+e.getMessage()); } finally { System.out.println("In finally"); } } }
- Exception happened / by zero In finally
- Exception happened / by zero
- In finally
- Compiler Error
#include<stdio.h> int main() { int p[]={1,2,3}; printf("%d %d",p,(&p)+1); } If address of p is -123456 and int occupies 4 bytes then what would be the output?
- Compile Time Error
- -123456, -123460
- -123456, -123444
- -123456, -123452
- None of the above
#include<stdio.h> int main () { ;;;;;; " Please Execute Me " ;;;;; // Line 6 printf(" Executing... ") // Line 7 ;;;;;; "Executed" ;;;;;;; // Line 8 }
- Executing...
- Please Execute Me Executing... Executed
- Error at line 6
- Error Semicolon missing
#include<stdio.h> int main() { printf("%d %f",6/4,6/4); }
- 1 1.000000
- 1 1.500000
- 0 1.000000
- 1 0.000000
#include<stdio.h> int main() { int i; if(i % 2) printf("I is Odd"), printf("I is Even"); }
- Error : Semicolon Missing
- Error: Misplaced Else
- I is odd
- I is odd I is Even
- No Output
#include<stdio.h> int main() { float dennis; switch(dennis) { default: printf("Let me work"); case 1: printf("This is case 1"); case 2: printf("This is case 2"); break; } }
- Let me work
- Let me workThis is case 1This is case 2
- Compilation Error
- Run Time Error
#include<stdio.h> int main() { char s1[]="Dennis"; char s2[]="Dennis"; if(s1 == s2) printf(" The man behind C"); else printf(" Experience the legacy of C"); }
- The man behind C
- Experience the legacy of C
- Error : == operator cannot be applied to reference data types
- boolean cannot be converted to int
#include<stdio.h> int main() { int i=0; for(;i--;); printf("%d",i); }
- 0
- 1
- Infinite loop
- -1
#include<stdio.h> int main() { int main=7; printf("%d",main); }
- 777777.........
- Compilation Error
- 7
- None of the above
#include<stdio.h> int factorial(int n) { (n == 0)? return 1 : return n* factorial(n-1); } int main() { printf("%d",factorial(3)); }
- 6
- Errror
- 1
- No output
C was given along with which operating system?
- DOS
- unix
- Solaris
- Both 1 and 2
package testswitch; public class TestSwitch { public static void main(String[] args) { int x = 2; switch (x) { default: System.out.println("Default"); break; case 3: System.out.println("I am in 3"); break; case 2: System.out.println("I am in 2"); break; } } }
- I am in 2
- Default I am in 2
- Default
- Compiler Error
package testswitch; public class TestSwitch1 { public static void main(String[] args) { int m=100; switch(100) { case m: System.out.println("in m"); default :System.out.println("default"); } } }
- in m Default
- Compiler Error
- in m
- default
package testswitch; public class TestSwitch3 { public static void main(String[] args) { long p = 10; final long q = 100; switch (p) { case q: System.out.println("in q"); break; case 10: System.out.println("in 10"); break; default: System.out.println("default"); } } }
- in 10 Default
- Compiler error
- in 10
- Exception
package testswitch; public class TestSwitch4 { public static void main(String[] args) { int x = 2; switch (x) { default: System.out.println("Default"); case 3: System.out.println("I am in 3"); case 1: System.out.println("I am in 1"); } } }
- Default I am in 3 I am in 1
- Default
- No output
- Exception
package testswitch; public class TestSwitch5 { public static void main(String[] args) { char a = 'a'; switch (a) { default: System.out.println("I am in default"); break; case 'a': System.out.println("I am in a"); break; } } }
- I am in a
- I am in default
- Exception
- Compiler Error
Given: class Scoop { static int thrower() throws Exception { return 42; } public static void main(String [] args) { try { int x = thrower(); } catch (Exception e) { X++; } finally { System.out.printIn("x = " + ++x); } } } What is the result?
- x = 42
- x = 43
- x = 44
- Compilation fails.
- The code runs with no output