InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
Write PL/SQL program to find the sum of digits of a number. |
|
Answer» DECLARE--Declare variables num, sum_of_digits and REMAINDER of datatype Integer num INTEGER; sum_of_digits INTEGER; remainder INTEGER; BEGINnum := 123456; sum_of_digits := 0;-- Find the sum of digits until original number DOESNT become null WHILE num <> 0 LOOP remainder := MOD(num, 10); sum_of_digits := sum_of_digits + remainder; num := TRUNC(num / 10); END LOOP; dbms_output.PUT_LINE('Sum of digits is '|| sum_of_digits); END; Input: 9874 |
|
| 2. |
Write PL/SQL program to convert each digit of a given number into its corresponding word format. |
|
Answer» DECLARE-- declare necessary VARIABLES -- num represents the GIVEN number -- number_to_word represents the word format of the number -- str, len and digit are the intermediate variables used for program executionnum INTEGER; number_to_word VARCHAR2(100); digit_str VARCHAR2(100); len INTEGER; digit INTEGER; BEGIN num := 123456; len := LENGTH(num); dbms_output.PUT_LINE('Input: ' ||num); -- Iterate through the number one by one FOR i IN 1..len LOOP digit := SUBSTR(num, i, 1); -- Using DECODE, get the str REPRESENTATION of the digit SELECT Decode(digit, 0, 'Zero ', 1, 'One ', 2, 'Two ', 3, 'Three ', 4, 'Four ', 5, 'Five ', 6, 'Six ', 7, 'Seven ', 8, 'Eight ', 9, 'Nine ') INTO digit_str FROM dual; -- Append the str representation of digit to final result. number_to_word := number_to_word || digit_str; END LOOP; dbms_output.PUT_LINE('Output: ' ||number_to_word); END; Input: 12345 |
|
| 3. |
Write a PL/SQL code to find whether a given string is palindrome or not. |
|
Answer» DECLARE-- Declared variables string, letter, reverse_string where string is the original string. string VARCHAR2(10) := 'abccba'; letter VARCHAR2(20); reverse_string VARCHAR2(10); BEGINFOR i IN REVERSE 1..LENGTH(string) LOOP letter := SUBSTR(string, i, 1); -- concatenate letter to reverse_string variable reverse_string := reverse_string ||''||letter; END LOOP;IF reverse_string = string THEN dbms_output.Put_line(reverse_string||''||' is palindrome'); ELSE dbms_output.Put_line(reverse_string ||'' ||' is not palindrome'); END IF; END; |
|
| 4. |
Write PL/SQL code block to increment the employee’s salary by 1000 whose employee_id is 102 from the given table below. |
||||||||||||||||||||||||||||||||||||||||
Answer»
|
|||||||||||||||||||||||||||||||||||||||||
| 5. |
Write a PL/SQL code to count the number of Sundays between the two inputted dates. |
|
Answer» --declare 2 DATES of type DateDECLARE start_date Date; end_date Date; sundays_count Number:=0; BEGIN -- input 2 dates start_date:='&input_start_date'; end_date:='&input_end_date'; /* Returns the date of the first day after the mentioned date and matching the day specified in second PARAMETER. */ start_date:=NEXT_DAY(start_date-1, 'SUNDAY'); --check the condition of dates by USING while loop. while(start_date<=end_date) LOOP sundays_count:=sundays_count+1; start_date:=start_date+7; END LOOP; -- print the count of sundays dbms_output.put_line('Total number of Sundays between the TWO dates:'||sundays_count); END; / Input: Output: |
|
| 6. |
Write a PL/SQL procedure for selecting some records from the database using some parameters as filters. |
Answer»
|
|
| 7. |
Write a PL/SQL program using WHILE loop for calculating the average of the numbers entered by user. Stop the entry of numbers whenever the user enters the number 0. |
|
Answer» DECLARE n NUMBER; average NUMBER :=0 ; sum NUMBER :=0 ; count NUMBER :=0 ;BEGIN -- TAKE INPUT from user n := &input_number; WHILE(n<>0) LOOP -- Increment count to find total elements count := count+1; -- Sum of elements ENTERED sum := sum+n; -- Take input from user n := &input_number; END LOOP; -- Average calculation average := sum/count; DBMS_OUTPUT.PUT_LINE(‘Average of entered NUMBERS is ’||average);END; |
|