Tag: programming languages

Questions Related to programming languages

  1. A 12

  2. B 012

  3. C 10

  4. D 10.0


Correct Option: C
Explanation:

To solve this question, the user needs to know the basics of Java programming language. The code creates an object of the Oct class and then calls the amethod() method, which initializes an integer variable oi with 012 and prints its value to the console.

In Java, leading zero in an integer literal denotes an octal (base-8) number. Therefore, the value 012 is an octal number that represents the decimal value 10.

So, the correct answer is:

The Answer is: C. 10

13 Which of the following statements are true?

  1. a. The elements in a Java array can only be of primitive types, not objects

  2. b. Arrays elements are initialized to default values wherever they are created using the keword new

  3. c. An array may be dynamically resized using the setSize method

  4. d. You can find out the size of an array using the size method


Correct Option: B
  1. a float f=1/3;

  2. b int i=1/3;

  3. c float f=1.01;

  4. d double d=999d;


Correct Option: A,B,D
  1. A System.out.println(1+1);

  2. B int i=2+'2';

  3. C String s="on"+'one';

  4. D byte b=255;


Correct Option: A,B

16 Which of the following statements are true?

  1. a. All of the variables in an interface are implicitly static

  2. b. All of the variables in an interface are implicitly final

  3. c. All of the methods in an interface are implicitly abstract

  4. d. A method in an interface can access class level variables


Correct Option: A,B,C

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) All of the variables in an interface are implicitly static - This option is true. In an interface, all variables are implicitly static, meaning they belong to the interface itself rather than any specific instance.

Option B) All of the variables in an interface are implicitly final - This option is true. In an interface, all variables are implicitly final, meaning their values cannot be changed once they are assigned.

Option C) All of the methods in an interface are implicitly abstract - This option is true. In an interface, all methods are implicitly abstract, meaning they do not have a body and must be implemented by any class that implements the interface.

Option D) A method in an interface can access class-level variables - This option is false. Methods in an interface can only access variables declared within the interface itself. They cannot access variables from implementing classes or any other classes.

The correct answer is A, B, C. These options are correct because variables in an interface are implicitly static and final, and methods in an interface are implicitly abstract.

  1. a. An interface can only contain method and not variables

  2. b. Interfaces cannot have constructors

  3. c. A class may extend only one other class and implement only one interface

  4. d. Interfaces are the Java approach to addressing its lack of multiple inheritance, but require implementing classes to create the functionality of the Interfaces.


Correct Option: B

18 You have a public class called myclass with the main method defined as follows public static void main(String parm[]){ System.out.println(parm[0]); } If you attempt to compile the class and run the program as follows java myclass hello What will happen?

  1. a. Compile time error, main is not correctly defined

  2. b. Run time error, main is not correctly defined

  3. c. Compilation and output of java

  4. d. Compilation and output of hello


Correct Option: D
  1. a. the code will compile an print "Equal".

  2. b. the code will compile an print "Not Equal".

  3. c. the code will cause a compiler error.

  4. d.


Correct Option: B

20 Read this piece of code carefully if(" String ".trim() == "String") System.out.println("Equal"); else System.out.println("Not Equal");

  1. a. the code will compile an print "Equal".

  2. b. the code will compile an print "Not Equal".

  3. c. the code will cause a compiler error

  4. d.


Correct Option: A

AI Explanation

To answer this question, let's go through each option and analyze the code snippet provided:

Option A) The code will compile and print "Equal". In the given code snippet, the trim() method is used to remove leading and trailing whitespace from the string " String ". The trim() method returns a new string with the whitespace removed.

However, when comparing strings, the == operator checks for reference equality, not value equality. In Java, the == operator compares the memory addresses of the objects being compared.

In this case, the string " String " is not the same object as the string "String", even though they have the same value after trimming. Therefore, the == comparison will evaluate to false.

So, the code will not print "Equal".

Option B) The code will compile and print "Not Equal". As explained in Option A, the == comparison will evaluate to false because the strings being compared are different objects. Therefore, the code will print "Not Equal".

Option C) The code will cause a compiler error. There is no compilation error in the given code. The code is syntactically correct, and there are no issues that would cause a compiler error.

Option D) (No option provided) This is not a valid option.

The correct answer is A) The code will compile and print "Equal".