Tag: programming languages

Questions Related to programming languages

  1. Web class

  2. Page class

  3. Data class

  4. Html classs


Correct Option: B

Which of the following lines will compile without warning or error.

  1. float f=1.3;

  2. char c="a";

  3. byte b=257;

  4. int i=10;


Correct Option: D

A byte can be of what size?

  1. -128 to 127

  2. (-2 power 8 )-1 to 2 power 8

  3. -255 to 256

  4. depends on the particular implementation of the Java Virtual machine


Correct Option: A

AI Explanation

To answer this question, we need to understand the concept of a byte and its size.

A byte is a unit of digital information that consists of 8 bits. Each bit can either be a 0 or a 1. Therefore, a byte can represent 2^8 (256) different values.

Option A) -128 to 127 - This option is correct. In a byte, the range of values is typically represented as -2^7 to 2^7 - 1. This means that the minimum value that can be stored in a byte is -128 (-2^7) and the maximum value is 127 (2^7 - 1).

Option B) (-2^8)-1 to 2^8 - This option is incorrect. The range mentioned in this option is incorrect. The correct range should be from -2^7 to 2^7 - 1.

Option C) -255 to 256 - This option is incorrect. The range mentioned in this option is incorrect. The correct range should be from -128 to 127.

Option D) depends on the particular implementation of the Java Virtual Machine - This option is incorrect. The size of a byte is standardized across different programming languages and platforms. It is always 8 bits.

Based on the explanations above, the correct answer is option A. A byte can represent values from -128 to 127.

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]); } }

  1. myprog

  2. good

  3. morning

  4. Exception raised: "java.lang.ArrayIndexOutOfBoundsException: 2"


Correct Option: D

AI Explanation

To answer this question, let's go through the code:

public class myprog {
    public static void main(String argv[]) {
        System.out.println(argv[2]);
    }
}

The main method is the entry point of the program. It takes a String array called argv as a parameter, which represents the command-line arguments passed to the program.

In the given command line: java myprog good morning, the arguments passed are "myprog", "good", and "morning".

The line System.out.println(argv[2]); attempts to print the element at index 2 of the argv array. In Java, arrays are zero-indexed, which means the first element is at index 0, the second element is at index 1, and so on.

However, in this case, the argv array only has 3 elements: "myprog", "good", and "morning". Since there is no element at index 2, an ArrayIndexOutOfBoundsException will be raised.

Therefore, the correct answer is:

D. Exception raised: "java.lang.ArrayIndexOutOfBoundsException: 2"

Which of the following are keywords or reserved words in Java?

  1. if

  2. then

  3. goto

  4. case


Correct Option: A,C,D

What will be printed out if you attempt to compile and run the following code ? int i=1; switch (i) { case 0: System.out.println("zero"); break; case 1: System.out.println("one"); case 2: System.out.println("two"); default: System.out.println("default"); }

  1. one

  2. one, default

  3. one, two, default

  4. default


Correct Option: C

Given the following declarations String s1=new String("Hello"); String s2=new String("there"); String s3=new String(); Which of the following are legal operations?

  1. s3=s1 + s2;

  2. s3=s1-s2;

  3. s3=s1 & s2;

  4. s3=s1 && s2


Correct Option: A

AI Explanation

To answer this question, you need to understand the operations that can be performed on strings in Java. Let's go through each option to understand why it is correct or incorrect:

Option A) s3 = s1 + s2 - This option is correct because the + operator can be used to concatenate strings in Java. It concatenates the string represented by s1 with the string represented by s2 and assigns the result to s3.

Option B) s3 = s1 - s2 - This option is incorrect because the - operator cannot be used with strings in Java. It is used for arithmetic operations and is not applicable to strings.

Option C) s3 = s1 & s2 - This option is incorrect because the & operator is a bitwise operator and cannot be used with strings in Java. It is used for bitwise operations on integers.

Option D) s3 = s1 && s2 - This option is incorrect because the && operator is a logical operator and cannot be used with strings in Java. It is used for logical operations on boolean values.

The correct answer is A. This option is correct because the + operator can be used to concatenate strings in Java.

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?

  1. Set

  2. List

  3. Map

  4. Vector


Correct Option: A