Multiple choice technology databases

Select incorrect variable declarations

  1. foo_text varchar2(10) := 'hello world';

  2. foo_char char(1) := 'Y';

  3. foo_number varchar2(10);

  4. foo_text number(10);

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

The incorrect variable declarations are A

  • A. foo_text varchar2(10) := 'hello world'; is incorrect because the variable foo_text is declared as a varchar2 type, but the initial value 'hello world' is a string. A varchar2 type can only store a sequence of characters, while a string can store a sequence of characters and other special characters, such as spaces and symbols.

The correct variable declarations are B, C, D.

  • B. foo_char char(1) := 'Y'; is correct because the variable foo_char is declared as a char type, which is a special type of varchar2 type that can only store a single character. The initial value 'Y' is a single character, so it is a valid value for the foo_char variable.
  • C. foo_number varchar2(10); is correct because the variable foo_number is declared as a varchar2 type, which can store a sequence of characters. The initial value 'hello world' is a sequence of characters, so it is a valid value for the foo_number variable.
  • D. foo_text number(10); is correct because the variable foo_text is declared as a number type, which can store an integer

Therefore, the correct answer is A.

AI explanation

Correct answer: foo_text varchar2(10) := 'hello world';

'hello world' is 11 characters, which exceeds the declared VARCHAR2(10) limit — assigning it at declaration time raises a runtime error (ORA-06502: character string buffer too small), so this declaration/initialization is invalid.

The other three are all valid: foo_char char(1) := 'Y'; assigns a single character to a CHAR(1), which fits; foo_number varchar2(10); declares a VARCHAR2 with no initial value, which is legal; and foo_text number(10); declares a NUMBER with no initializer, also legal (the variable name being "foo_text" is just a misleading label, not a type mismatch).