Java and C Programming Fundamentals
Test your knowledge of Java and C programming including inheritance, packages, operators, data types, arrays, and language-specific syntax.
Questions
What is the output of the following code snippet ?? void main() { int a = (2,4); printf("%d", a); }
- 2
- compiler error
- runtime error
- 4
- 0
What is the output of following code snippet ? void main() { printf(printf("anna")); }
- anna
- compile time error
- anna4
- non of the above
- garbage
What is the output of following code snippet ? void main() { int arr[5]={10,20,30,40,50}; printf("%d",(arr+2)); }
- 30
- 20
- syntax error
- garbage
What is the output ?? main() { float me = 1.1; double you = 1.1; if(me==you) printf("I love U"); else printf("I hate U"); }
- I hate U
- I love U
- non of the above
- error
What is the output ? main() { char *p; printf("%d %d ",sizeof(*p),sizeof(p)); }
- 2 2
- 1 2
- compiler error
- non of the above
Which of the access modifiers a top level class may have?
- protected
- private
- public
- friendly
How many bytes are used to represent the primitive data type int in Java ?
- 4
- 2
- 8
- The number of bytes to represent an int is compiler dependent.
Given a one dimensional array arr, what is the correct way of getting the number of elements in arr.
- arr.length() – 1
- arr.length()
- arr.length - 1
- arr.length
What is the legal range of values for a variable declared as a byte?
- 0 to 256
- -128 to 127
- -127 to 128
- 0 to 255
In the following class definition, which is the first line (if any) that causes a compilation error. Select the one correct answer.public class test { public static void main(String args[]) { char c; int i; c = 'A'; // 1 i = c; //2 c = i + 1; //3 c++; //4 }}
- The line labeled 1.
- The line labeled 2.
- The line labeled 3.
- All the lines are correct and the program compiles.
Which of the following are keywords in Java?
- synchronized
- friend
- implement
- throws
What gets printed when the following program is compiled and run? class test { public static void main(String args[]) { int i,j,k,l=0; k = l++; j = ++k; i = j++; System.out.println(i); } }
- 1
- 3
- 2
- 0
What gets printed when the following program is compiled and run. protected class example {public static void main(String args[]) {String test = "abc";test = test + test;System.out.println(test);}}
- The program does not compile because statement "test = test + test" is illegal.
- The program prints "abc"
- The program prints "abcabc"
- The class does not compile because the top level class cannot be protected.
What gets printed when the following program is compiled and run. Select the one correct answer class B { public static void main(String[] args) { int a=10; if(a=10) System.out.println("tomcat"); else System.out.println("root"); } }
- tomcat
- root
- Compile time Error
- Run Time Error
package inheritance; class TestInheritance1 { public int empId =100; public String name ="tcs" ; } package inheritance1; import inheritance.TestInheritance1; public class TestInheritance_1 extends TestInheritance1 { public static void main(String[] args) { System.out.println("EmpId is "+empId); } }
- EmpId is 100
- Compiler error
- EmpId is 0
- Exception
package inheritance; public class TestInheritance2 { public int empId =100; public String name ="tcs" ; } package inheritance1; import inheritance.TestInheritance2; public class TestInheritance_2 extends TestInheritance2 { void printEmpId() { System.out.println("EmpId is "+empId); } public static void main(String[] args) { TestInheritance_2 T_2 = new TestInheritance_2(); T_2.printEmpId(); } }
- Compiler Error
- EmpId is 0
- EmpId is 100
- Exception
package inheritance; public class TestInheritance3 { protected int p = 100; } package inheritance1; import inheritance.TestInheritance3; public class TestInheritance_3 extends TestInheritance3 { void printP() { System.out.println("p= "+p); } public static void main(String[] args) { TestInheritance_3 T_3 = new TestInheritance_3(); T_3.printP(); } }
- p= 0
- p= 100
- Compiler Error
- Exception
package inheritance; class TestInheritance4 { String name ="TCS"; } package inheritance1; import inheritance.TestInheritance4; public class TestInheritance_4 extends TestInheritance4 { void printName() { System.out.println("name ="+name); } public static void main(String[] args) { TestInheritance_4 T_4 = new TestInheritance_4(); T_4.printName(); } }
- Compiler Error
- name = TCS
- name = null
- Exception
package inheritance; public class TestInheritance7{ protected int p = 100; } package inheritance1; import inheritance.TestInheritance7; public class TestInheritance_7 extends TestInheritance7{ void printP() { TestInheritance7 T7 = new TestInheritance7(); System.out.println("p = "+T7.p); } public static void main(String[] args) { TestInheritance_7 T_7 = new TestInheritance_7(); T_7.printP(); } }
- p = 100
- Compiler error
- p = 0
- Exception
package inheritance;
class TestInheritance4 {
String name = "JAVA";
}
package inheritance1;
import
import inheritance.TestInheritance4;
public class TestInheritance_4 extends TestInheritance4 {
void printName() {
System.out.println("name =" + name);
}
public static void main(String[] args) {
TestInheritance_4 T_4 = new TestInheritance_4();
T_4.printName();
}
}
- name = JAVA
- Compiler Error
- Exception
- name = null