Free LPI 102-500 Exam Questions (page: 3)

What is the difference between the commands test -e path and test -f path?

  1. They are equivalent options with the same behaviour.
  2. The -f option tests for a regular file. The -e option tests for an empty file.
  3. Both options check the existence of the path. The -f option also confirms that it is a regular file.
  4. The -f option tests for a regular file. The -e option tests for an executable file.

Answer(s): C

Explanation:

The test command is used to perform checks and comparisons on files and values. The -e option tests if a given path exists, regardless of its type (file, directory, link, etc.). The -f option tests if a given path exists and is a regular file, not a directory or a special file. For example, if we have a directory named dir and a file named file, we can use the test command as follows:
test -e dir && echo "dir exists" dir exists test -f dir && echo "dir is a regular file" (no output) test -e file && echo "file exists" file exists test -f file && echo "file is a regular file" file is a regular file


Reference:

https://www.howtoforge.com/linux-test-command/ https://www.computerhope.com/unix/bash/test.htm



How can the existing environment variable FOOBAR be suppressed for the execution of the script./myscript only?

  1. unset -v FOOBAR;./myscript
  2. set -a FOOBAR="";./myscript
  3. env -u FOOBAR./myscript
  4. env -i FOOBAR./myscript

Answer(s): C

Explanation:

The env command can be used to run a utility or command in a custom environment without having to modify the currently existing environment1. The -u or --unset option can be used to remove a variable from the environment12. Therefore, the command env -u FOOBAR./myscript will run the script./myscript in an environment where the variable FOOBAR is suppressed. The other options are incorrect for the following reasons:
A . unset -v FOOBAR;./myscript: This will unset the variable FOOBAR in the current shell, not just for the script execution. The semicolon (;) separates two commands, so the script will run in the same environment as the unset command.
B . set -a FOOBAR="";./myscript: This will set the variable FOOBAR to an empty string, not suppress it. The -a option means that the variable will be exported to the environment of subsequent commands, so the script will still see the variable FOOBAR, but with no value. D . env -i FOOBAR./myscript: This will run the script in an empty environment, not just suppress the variable FOOBAR. The -i or --ignore-environment option means that no environment variables will be passed to the command12.


Reference:

env command in Linux with Examples - GeeksforGeeks, env - Wikipedia.



When the command echo $$ outputs 12942, what is the meaning of 12942?

  1. It is the process ID of the echo command.
  2. It is the process ID of the current shell.
  3. It is the process ID of the last command executed.
  4. It is the process ID of the last command which has been placed in the background.

Answer(s): B

Explanation:

In bash, the PID of a shell script's subshell process is stored in a special variable called $$. This variable is read-only, and you cannot modify it in a shell script1. You can use echo $$ to get the PID of the current bash shell you are using2. Therefore, when the command echo $$ outputs 12942, it means that the PID of the current shell is 12942.


Reference:

[LPI Linux Essentials - Topic 103: Command Line Basics]
[Bash Special Parameters]
How to get the process ID (PID) of a shell script
How to know the process id of current bash session?



What output will the following command produce?

seq 1 5 20

  1. 1
  2. 1
  3. 1
  4. 2
  5. 5

Answer(s): B

Explanation:

The seq command in Linux is used to generate a sequence of numbers from FIRST to LAST in steps of INCREMENT1. The syntax for the seq command is:
seq [OPTION]... LAST or seq [OPTION]... FIRST LAST or seq [OPTION]... FIRST INCREMENT LAST In this case, the command seq 1 5 20 has three arguments: FIRST = 1, INCREMENT = 5, and LAST = 20. This means that the command will produce numbers from 1 to 20 in steps of 5. The output will be:
1 5 10 15
The output will not include 20 because it is not a multiple of 5. The output will be printed on separate lines by default, unless a different separator is specified with the -s option2.


Reference:

Seq Command in Linux [Explained With Examples]
seq Man Page - Linux - SS64.com - SS64 Command line reference



Which of the following words is used to restrict the records that are returned from a SELECT SQL query based on a supplied criteria for the values in the records?

  1. CASE
  2. FROM
  3. WHERE
  4. IF

Answer(s): C

Explanation:

The SQL WHERE clause is used to restrict the records that are returned from a SELECT SQL query based on a supplied criteria for the values in the records12. The WHERE clause follows the SELECT and FROM clauses and contains one or more conditions that must be true for a record to be included in the result set. The general syntax of the WHERE clause is:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
The condition can be a comparison, a logical operation, a pattern matching, a subquery, or a combination of these using various operators12. For example, the following query selects all the records from the customers table where the country is `USA':
SELECT * FROM customers
WHERE country = 'USA';
The other words listed in the question are not used to filter records based on values. They have different meanings and purposes in SQL:
CASE: This is a conditional expression that returns a value based on a set of conditions3. It can be used in SELECT, UPDATE, DELETE, or WHERE statements. For example, the following query uses a CASE expression to assign a rating to each customer based on their credit limit:
SELECT customer_name, credit_limit, CASE WHEN credit_limit > 10000 THEN `High' WHEN credit_limit > 5000 THEN `Medium' ELSE `Low' END AS rating FROM customers; FROM: This is a clause that specifies the table (s) or view (s) from which the data is retrieved. It follows the SELECT clause and precedes the WHERE clause. For example, the following query selects the customer name and order date from the customers and orders tables:
SELECT customer_name, order_date FROM customers JOIN orders ON customers.customer_id = orders.customer_id;
IF: This is a control flow statement that executes a block of code based on a condition. It can be used in stored procedures, functions, triggers, or batch files. For example, the following code snippet uses an IF statement to check if a variable is positive or negative:
DECLARE @num INT; SET @num = -10; IF @num > 0 BEGIN PRINT `Positive'; END ELSE BEGIN PRINT `Negative'; END


Reference:

1: SQL WHERE Clause - W3Schools
2: How to Write a WHERE Clause in SQL | LearnSQL.com
3: [SQL CASE Statement - W3Schools] : [SQL FROM Clause - W3Schools] : [SQL IF...ELSE Statement - W3Schools]



Which of the following commands lists all defined variables and functions within Bash?

  1. env
  2. set
  3. env -a
  4. echo $ENV

Answer(s): B

Explanation:

The set command lists all defined variables and functions within Bash, including local, environment, and shell variables, as well as aliases and functions. The output of set can be very long, so it is often piped to less, grep, or other commands for filtering or paging. The set command can also be used to set or unset shell options and positional parameters. The -o posix option to set limits the output to only variables, as defined by the POSIX standard123.
The env command lists only the environment variables, which are a subset of the shell variables that are passed to child processes. The env command can also be used to run a command in a modified environment, or to print or set environment variables. The -a option to env is not valid in most implementations45.
The echo command prints a line of text to the standard output. The $ENV variable is not a predefined variable in Bash, but it can be set by the user or by other programs. If it is not set, echo $ENV will print a blank line1 .



Which of the following SQL queries counts the number of occurrences for each value of the field order_type in the table orders?

  1. SELECT order_type,COUNT(*) FROM orders WHERE order_type=order_type;
  2. SELECT order_type,COUNT(*) FROM orders GROUP BY order_type;
  3. COUNT(SELECT order_type FROM orders);
  4. SELECT COUNT(*) FROM orders ORDER BY order_type;
  5. SELECT AUTO_COUNT FROM orders COUNT order_type;

Answer(s): B

Explanation:

The correct SQL query to count the number of occurrences for each value of the field order_type in the table orders is:
SELECT order_type,COUNT(*) FROM orders GROUP BY order_type; This query uses the SELECT statement to retrieve the values of the order_type field and the COUNT(*) function to count the number of rows for each order_type. The GROUP BY clause groups the rows by the order_type field, so that the count is calculated for each distinct value of order_type. The result of this query is a table with two columns: order_type and count, where each row shows the number of orders for a specific order_type.

The other options are incorrect for the following reasons:
A: This query uses a WHERE clause that is always true, since order_type=order_type for every row. Therefore, this query returns the same result as SELECT order_type,COUNT(*) FROM orders;, which is a table with one row that shows the total number of orders, regardless of the order_type.
C: This query is syntactically invalid, since the COUNT function cannot take a subquery as an argument. The correct way to use a subquery with COUNT is COUNT((SELECT order_type FROM orders));, which returns the total number of orders, regardless of the order_type.
D: This query uses the ORDER BY clause to sort the rows by the order_type field, but it does not group them by order_type. Therefore, this query returns the same result as SELECT COUNT(*) FROM orders;, which is a table with one row that shows the total number of orders, regardless of the order_type.
E: This query is syntactically invalid, since there is no such function as AUTO_COUNT in SQL, and the COUNT function cannot take a field name as an argument. The correct way to use COUNT with a field name is COUNT(order_type);, which returns the number of non-null values in the order_type field.


Reference:

[SQL COUNT Function]
[SQL GROUP BY Statement]
[SQL SELECT Statement]



What is the purpose of the file /etc/profile?

  1. It contains the welcome message that is displayed after login.
  2. It contains security profiles defining which users are allowed to log in.
  3. It contains environment variables that are set when a user logs in.
  4. It contains default application profiles for users that run an application for the first time.

Answer(s): C

Explanation:

The file /etc/profile is a configuration file that is read by the Bash shell when a user logs in. It contains commands and settings that apply to all users of the system, such as environment variables, PATH information, terminal settings, and security commands. Environment variables are variables that affect the behavior of programs and processes. For example, the PATH variable defines the directories where the shell looks for executable files, and the JAVA_HOME variable defines the location of the Java installation. The /etc/profile file can also source other files from the /etc/profile.d/ directory, which can contain additional scripts for setting environment variables or other system-wide settings. The /etc/profile file is not the only file that can set environment variables for a user. There are also user-specific files, such as ~/.profile, ~/.bash_profile, and ~/.bashrc, that are read by the shell after /etc/profile. These files can override or append to the settings in /etc/profile, or define new variables for the user. The order and precedence of these files depend on the type of shell (login or interactive) and the options used to start the shell. You can learn more about the difference between these files here1 and here2.


Reference:

https://www.thegeekdiary.com/understanding-etc-profile-configuration-file-in-linux/

https://unix.stackexchange.com/questions/704610/what-does-the-etc-profile-do






Post your Comments and Discuss LPI 102-500 exam prep with other Community members:

102-500 Exam Discussions & Posts